vb.net “[Modulename]”的类型初始值设定项引发异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22242450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:27:20  来源:igfitidea点击:

The type initializer for '[Modulename]' threw an exception

vb.netexceptionmodule

提问by Juan Filipe

I have this new vb.net project (MedicalCost) having this error "The type initializer for 'MedicalCost.Constants' threw an exception." when running the sub.. I've already done this before on my previous project and everything works fine when I'm declaring ang public varible on my module but now in my current project its not working.

我有这个新的 vb.net 项目 (MedicalCost) 有这个错误“'MedicalCost.Constants' 的类型初始值设定项抛出异常。” 运行子时..我之前已经在我以前的项目中做过这个,当我在我的模块上声明ang公共变量时一切正常,但现在在我当前的项目中它不起作用。

here is my code on my module(Constants):

这是我的模块上的代码(常量):

 Imports System.Data.Sql
    Imports System.Data.SqlClient
    Imports System.Data.Odbc
    Imports System.Windows.Forms

    Public Module Constants
        Public ppiconn As New SqlConnection("Dsn=pandiman2002connectdsn;server=ppi;uid=sa;database=Pandimandata2002")
        'Dsn=pandiman2002connectdsn;description=PPI Database;uid=sa;app=Microsoft? Visual Studio? 2010;wsid=CRWUSER17-PC;database=Pandimandata2002
        Public da As New SqlDataAdapter
        Public comm As New SqlCommand
        Public dr As SqlDataReader
        Public ds As New DataSet

        Public x As String


    End Module

when im running the sub on my frm_add

当我在我的 frm_add 上运行 sub 时

here is my code

这是我的代码

Sub search_crew()

    Try
        x = "(isnull(ltrim(rtrim(firstname)),'') + ' ' + isnull(ltrim(rtrim(mi)),'') + ' ' + " _
               & "isnull(ltrim(rtrim(lastname)),'') like '%" & Replace(searchbox.Text, " ", "%") & "%' " _
               & " or isnull(ltrim(rtrim(lastname)),'') + ' ' + isnull(ltrim(rtrim(mi)),'') + ' ' + " _
               & "isnull(ltrim(rtrim(firstname)),'') like '%" & Replace(searchbox.Text, " ", "%") & "%' " _
               & "or legal_records.caseno like '%" & UCase(searchbox.Text) & "%')"

        ppiconn.Close()
        ppiconn.Open()
        Pandimandata2002DataSet.EnforceConstraints = False
        da = New SqlDataAdapter(select_tblcrw & "where " & x, ppiconn)
        da.Fill(Pandimandata2002DataSet.tblCrew)
        da.Dispose()
        ppiconn.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub

I noticed that any variable declared from module is the reason why this error "The type initializer for 'MedicalCost.Constants' threw an exception." occurs.

我注意到从模块声明的任何变量都是这个错误“'MedicalCost.Constants'的类型初始值设定项引发异常的原因。” 发生。

can anyone help me. I already spent 1 hour searching for this error and nothings helps me. tnx!

谁能帮我。我已经花了 1 个小时来搜索这个错误,但没有任何帮助。天!

回答by JaredPar

The problem is almost certainly the following line of code

问题几乎肯定是下面这行代码

Public ppiconn As New SqlConnection("Dsn=pandiman2002connectdsn;server=ppi;uid=sa;database=Pandimandata2002")

The key here is type initializerin the error message. That happens when the initialization of static data throws an exception. For VB.Net this maps to the fields of Modulesor Sharedfields of Class. In this case the error points to Constantsand this is the only initializer hence it is likely to blame.

这里的关键在于type initializer错误消息。当静态数据的初始化引发异常时会发生这种情况。对于 VB.Net,这映射到 的字段Modules或 的Shared字段Class。在这种情况下,错误指向Constants并且这是唯一的初始值设定项,因此它可能是罪魁祸首。

In order to find out why this happens you will need to find the exception which triggered the type initializererror. Simply debug into the application wait for the error, expand the InnerExceptionproperty and that should contain the real error

为了找出发生这种情况的原因,您需要找到触发type initializer错误的异常。只需调试到​​应用程序中等待错误,展开InnerException属性,应该包含真正的错误

回答by user7427506

Had this problem with Initializer in a module for Public definitions -- Moved the initializer To the Form using it -- Still as Public. The Problem disappeared.

在公共定义的模块中使用初始化器有这个问题——将初始化器移动到使用它的表单——仍然是公共的。问题消失了。

回答by Robert Cody

This error came up for me when I updated a reference to a 3rd-party DLL. The error "The type initializer for {myForm} threw an exception" appeared on creating the main form in the installed program and the program halted with the splash screen visible. The debugger was no help because the program ran correctly in the debugger. It turned out that my installer was not including some of the updated 3rd-party dependencies for the DLL. Once the updated dependencies were identified and added into the program folder by the installer, the program ran correctly.

当我更新对第 3 方 DLL 的引用时,出现了这个错误。在已安装的程序中创建主窗体时出现错误“{myForm} 的类型初始值设定项引发异常”,并且程序停止并显示初始屏幕。调试器没有帮助,因为程序在调试器中正确运行。事实证明,我的安装程序不包括 DLL 的一些更新的第 3 方依赖项。一旦更新的依赖项被安装程序识别并添加到程序文件夹中,程序就会正确运行。