共享变量的访问将抛出对象引用错误。

时间:2021-07-06 23:34:47

I have a shared variable in a Form.

我有一个形式的共享变量。

Public Class FormHome
      Public Shared db_config As Dictionary(Of String, String)

When I try to access this variable from another form,

当我尝试从另一种形式访问这个变量时,

FormHome.db_config.ContainsKey("m")

It works fine in my system (Windows 7). But I get object reference error (System.NullReferenceException) in Windows XP (client system) in exactly that line.

它在我的系统中运行良好(Windows 7),但是在Windows XP(客户端系统)中,我得到了对象引用错误(system . nullreferenceexception)。

In both places, .NET 4.0 framework (Version - 4.0.30319.18034) is used by the application. Seems weird. What causes this error?

在这两个地方,应用程序都使用。net 4.0框架(版本- 4.0.30319.18034)。似乎是奇怪的。导致这个错误的原因是什么?

1 个解决方案

#1


0  

This code

这段代码

Public Shared db_config As Dictionary(Of String, String)

only declares a variable (db_config) of the requested type. It does not create an object of that type.

只声明请求类型的变量(db_config)。它不会创建该类型的对象。

Before you create the object, db_config is still null, and thus attempting to access it will result in a NullReferenceException.

在创建对象之前,db_config仍然为空,因此尝试访问它将导致NullReferenceException。

To create an object, you need to use the New keyword. For example:

要创建对象,需要使用新的关键字。例如:

db_config = New Dictionary(Of String, String)()

You need to make sure that this object creation happens before the first attempt is made to access the variable's value. Because you're using a static (Shared) variable, doing it in the class's instance constructor may be too late. The regular instance constructor only gets executed when you create an instance of the object.

您需要确保在第一次尝试访问变量的值之前创建对象。因为您使用的是静态(共享)变量,所以在类的实例构造函数中执行它可能已经太晚了。只有当您创建对象的实例时,常规的实例构造函数才会被执行。

Therefore, I recommend doing it in the object's static constructor. As the language specification explains in more detail, the static constructor of a class is guaranteed to have been executed by the runtime by the time that any of the static members of that class is accessed.

因此,我建议在对象的静态构造函数中执行它。由于语言规范更详细地解释了,类的静态构造函数保证在访问该类的任何静态成员时被运行时执行。

Add the following code to your FormHome class:

将以下代码添加到FormHome类中:

Shared Sub New()
    db_config = New Dictionary(Of String, String)()
End Sub

#1


0  

This code

这段代码

Public Shared db_config As Dictionary(Of String, String)

only declares a variable (db_config) of the requested type. It does not create an object of that type.

只声明请求类型的变量(db_config)。它不会创建该类型的对象。

Before you create the object, db_config is still null, and thus attempting to access it will result in a NullReferenceException.

在创建对象之前,db_config仍然为空,因此尝试访问它将导致NullReferenceException。

To create an object, you need to use the New keyword. For example:

要创建对象,需要使用新的关键字。例如:

db_config = New Dictionary(Of String, String)()

You need to make sure that this object creation happens before the first attempt is made to access the variable's value. Because you're using a static (Shared) variable, doing it in the class's instance constructor may be too late. The regular instance constructor only gets executed when you create an instance of the object.

您需要确保在第一次尝试访问变量的值之前创建对象。因为您使用的是静态(共享)变量,所以在类的实例构造函数中执行它可能已经太晚了。只有当您创建对象的实例时,常规的实例构造函数才会被执行。

Therefore, I recommend doing it in the object's static constructor. As the language specification explains in more detail, the static constructor of a class is guaranteed to have been executed by the runtime by the time that any of the static members of that class is accessed.

因此,我建议在对象的静态构造函数中执行它。由于语言规范更详细地解释了,类的静态构造函数保证在访问该类的任何静态成员时被运行时执行。

Add the following code to your FormHome class:

将以下代码添加到FormHome类中:

Shared Sub New()
    db_config = New Dictionary(Of String, String)()
End Sub