vb.net VB中的静态类构造函数

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

Static class constructor in VB

c#vb.net

提问by AppFzx

Is there a way to make a constructor for a sharedclass in VB.NET? I do it all the time in C# as follows, but I can't seem to get it to work in VB.NET.

有没有办法为sharedVB.NET 中的类创建构造函数?我一直在 C# 中这样做,如下所示,但我似乎无法让它在 VB.NET 中工作。

static class someClass
{
    public static string somePublicMember;

    static someClass()
    {
        messageBox.show("I just constructed a static class");
    }
}

When the following code is executed, the constructor will be called.

执行以下代码时,将调用构造函数。

...
someSillyClass.someSillyPublicMember = 42;
...

Can a static(shared) class even have a constructor in VB.NET?

一个可以staticshared)类甚至在VB.NET构造?

采纳答案by Neolisk

You cannot declare a shared class in VB.NET. You have two options:

不能在 VB.NET 中声明共享类。您有两个选择:

  • use modules. In this case you need some Init, which you need to call before anything else.
  • use regular classes with Shared methods (my preference), then you can have shared sub new.
  • 使用模块。在这种情况下,您需要 some Init,您需要先调用它。
  • 使用具有共享方法的常规类(我的偏好),然后您可以共享子新。

回答by Ehsan

Read documentation here. In you can do

在此处阅读文档。在你可以做

Shared Sub New()
...
End Sub

And it will be invoked. From MSDN:

它将被调用。来自 MSDN:

  1. Shared constructors are run before any instance of a class type is created.

  2. Shared constructors are run before any instance members of a structure type are accessed, or before any constructor of a structure type is explicitly called. Calling the implicit parameter less constructor created for structures will not cause the shared constructor to run.

  3. Shared constructors are run before any of the type's shared members are referenced.

  4. Shared constructors are run before any types that derive from the type are loaded.

  5. A shared constructor will not be run more than once during a single execution of a program.

  1. 共享构造函数在创建类类型的任何实例之前运行。

  2. 共享构造函数在访问结构类型的任何实例成员之前或在显式调用结构类型的任何构造函数之前运行。调用为结构创建的隐式无参数构造函数不会导致共享构造函数运行。

  3. 共享构造函数在引用任何类型的共享成员之前运行。

  4. 共享构造函数在加载从该类型派生的任何类型之前运行。

  5. 在程序的单次执行期间,共享构造函数不会运行多次。

回答by Grant Thomas

Kind of looks like a normal constructor in VB.NET:

有点像 VB.NET 中的普通构造函数:

Shared Sub New()

End Sub

回答by Bill Gregg

Have you tried:

你有没有尝试过:

Class someClass

    Public Shared somePublicMember As String

    Shared Sub New()
        messageBox.show("I just constructed a static class")
    End Sub
End Class

回答by Wolfgang Grinfeld

There are no static/shared classes in VB.net.

VB.net 中没有静态/共享类。

There are however Modules that provide similar, thus you will not be able to instantiate them.

然而,有提供类似的模块,因此您将无法实例化它们。

Your equivalent code in VB.Net would be:

您在 VB.Net 中的等效代码将是:

Module someClass
    Public somePublicMember As String

    Sub New()
        messageBox.show("I just constructed a static class (not really) [sic]")
    End Sub
End Module