Search code examples
vb.netexceptiontypesmoduleinitializer

The type initializer threw an exception for one of my modules?


Module Upgradesupport
Friend RegistryFncs_definst as new registryFncs
Friend AccessApplication_definst As New Access.Application
Friend RDOrdoEngine_definst As New RDO.rdoEngine
Friend DAODBEngine_definst As New DAO.DBEngine    
end module

this is all the code for that module. it throws the exception when the form that uses it loads (thats when the module is called) i dont know what is causing the problem but it could be the third or fourth line.


Solution

  • Split the declaration from creating an instance something like this:

    Module Upgradesupport
        Friend RegistryFncs_definst as registryFncs
        Friend AccessApplication_definst As Access.Application
        Friend RDOrdoEngine_definst As RDO.rdoEngine
        Friend DAODBEngine_definst As DAO.DBEngine    
    End Module
    

    Removing the New reduces it to a declaration of assembly-wide Scope. Then in some related class, most logically in the constructor:

     Public Class SomeRelatedClass
        Public Sub New
            ' initialize objects
            RegistryFncs_definst = New registryFncs
            AccessApplication_definst  = New Access.Application
            RDOrdoEngine_definst = New RDO.rdoEngine
            DAODBEngine_definst = New DAO.DBEngine  
    
            ' other stuff you need
        End Sub  
    

    IF your app starts from a Sub Main rather than a main form, you could do the initialization in Sub Main:

    Public Sub Main
        ' initialize objects
        RegistryFncs_definst = New registryFncs
        AccessApplication_definst  = New Access.Application
        RDOrdoEngine_definst = New RDO.rdoEngine
        DAODBEngine_definst = New DAO.DBEngine
    End Sub