vb.net 以另一种形式访问类的同一个实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14419516/
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-17 11:57:26  来源:igfitidea点击:

Accessing the same instance of a class in another form

vb.netclasssingletoninstance

提问by Brady

I'm sure this is a simple question, but I don't have enough experience to know the answer. :)

我确定这是一个简单的问题,但我没有足够的经验来知道答案。:)

DataClass, Form1, Form2

I have a public class, DataClass, in a separate file, DataClass.vb. In DataClassI have data stored in several arrays that I need to access. I have methods in DataClassso that I can access the data. One of them is GetName. Everything works fine on Form1. I need to access the same data in the arrays on a another form, but I am required to call a new instance of the class, so when I call the methods to access the arrays, the data is empty.

DataClass在一个单独的文件中有一个公共类DataClass.vb. 在DataClass我有数据存储在我需要访问的几个数组中。我有方法DataClass可以访问数据。其中之一是GetName。一切正常Form1。我需要在另一个表单上访问数组中的相同数据,但是我需要调用类的新实例,因此当我调用方法访问数组时,数据为空。

I've seen some threads mention creating a singleton class, but most are about C# which I am not as familiar with.

我已经看到一些线程提到创建一个singleton class,但大多数是关于 C# 的,我不太熟悉。

What is the best practice?

最佳做法是什么?

回答by xfx

There are many ways in which you can do this. One of them would involve creating a Moduleand then making the variable that instantiates your class Publicinside the module:

有很多方法可以做到这一点。其中之一将涉及创建一个Module,然后创建Public在模块内实例化您的类的变量:

Module MyGlobalVariables
    Public MyDataClass As DataClass
End Module

Now, all the forms in your project will be able to access the DataClassvia MyGlobalVariables.MyDataClass.

现在,您项目中的所有表单都可以访问DataClassvia MyGlobalVariables.MyDataClass.



A preferable method would be to add a property to your Form2 that can be set to the DataClassinstance:

一个更好的方法是向您的 Form2 添加一个可以设置为DataClass实例的属性:

Public Property MyDataClass As DataClass

Then, you would instantiate your Form2as follows (assuming the variable you use to instantiate DataClassin Form1is called _dataClass):

然后,你会实例化Form2如下(假设你使用实例化的变量DataClassForm1被调用_dataClass):

Dim frm2 As New Form2()
frm2.MyDataClass = _dataClass
frm2.Show()


And finally, another way would be to override the constructor of Form2and allow it to receive a parameter of type DataClass. Then, you could instantiate Form2as:

最后,另一种方法是覆盖 的构造函数Form2并允许它接收类型为 的参数DataClass。然后,您可以实例Form2化为:

Dim frm2 As New Form2(_dataClass)

Hope this helps...

希望这可以帮助...

回答by Olivier Jacot-Descombes

You can create a singleton class like this

你可以像这样创建一个单例类

Public Class DataClass
    Public Shared ReadOnly Instance As New DataClass()

    Private Sub New()
    End Sub

    ' Other members here
End Class

You can access a single instance through the shared Instancemember which is initialized automatically. The constructor Newis private in order to forbid creating a new instance from outside of the class.

您可以通过Instance自动初始化的共享成员访问单个实例。构造函数New是私有的,以禁止从类外部创建新实例。

You can access the singleton like this

您可以像这样访问单身人士

Dim data = DataClass.Instance

But this is not possible

但这是不可能的

Dim data = new DataClass() 'NOT POSSIBLE!

Since the singleton class is accessed through the class name, you can access it from the two forms easily.

由于单例类是通过类名访问的,因此可以很容易地从两种形式访问它。