VB.NET中的静态方法实现

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

Static Method implementation in VB.NET

.netvb.netstatic

提问by Atanu Roy

I am confused with Staticimplementation in VB.NET. In C#, we can create Static class and static methods to write utility methods for our application.

Static对 VB.NET 中的实现感到困惑。在 C# 中,我们可以创建静态类和静态方法来为我们的应用程序编写实用程序方法。

Now, VB.NET lets us create Modulein place of static class. If we create a method in the module, by default it becomes static. But in my application, I have written the below code:

现在,VB.NET 让我们创建Module代替静态类。如果我们在模块中创建一个方法,默认情况下它会变成静态的。但是在我的应用程序中,我编写了以下代码:

Public Class Utility
    Public Shared Function GetValue() As String
       // My code
    End Function
End Class

By writing the code, I am able to access the utility method as Utility.GetValue(). As this is not a static class, I am supposed to instantiate an object of it. But this method is available for both the class and objects of Utility

通过编写代码,我能够以Utility.GetValue(). 由于这不是静态类,我应该实例化它的一个对象。但是这个方法对类和对象都可用Utility

Now my questions are:

现在我的问题是:

  1. Is the implementation I have done may violate any of the features of static class that module provide?
  2. What will be difference between this and implementing a module instead?
  3. If I create a module instead, will the scope of that will be same as this class? I want to access the method throughout the project as well as other projects where this one is referenced.
  1. 我所做的实现是否可能违反模块提供的静态类的任何功能?
  2. 这与实现模块有什么区别?
  3. 如果我创建一个模块,它的范围是否与此类相同?我想在整个项目以及引用此方法的其他项目中访问该方法。

I tried consulting multiple articles, but nowhere found this exact answers. Please help.

我尝试查阅多篇文章,但没有找到确切的答案。请帮忙。

回答by Mike Hofer

A VB.NET module isa static class. The compiler handles this for you. Every method and property on it is static(Shared).

VB.NET 模块一个静态类。编译器会为您处理这个问题。它的每个方法和属性都是static( Shared)。

A class with a static (Shared) member on it is exactly that: a class with a static (Shared) member. You don't have to create an instance of it to access the static (Shared) method, but you do to get to any of its instance members.

具有静态(共享)成员的类正是这样:具有静态(共享)成员的类。您不必创建它的实例来访问静态(共享)方法,但您必须创建它的任何实例成员。

You can also define a Sub New()in a module, and it becomes the static constructor for the module. The first time you try to invoke a member on the module, the static constructor will be invoked to initialize the static class.

您也可以Sub New()在模块中定义 a ,它成为模块的静态构造函数。第一次尝试调用模块上的成员时,将调用静态构造函数来初始化静态类。

回答by Doug Null

Use 'shared' to make a class sub or function 'static' (in the C# sense). In VB, shared is like a synonym for static, in this context.

使用“共享”使类子或函数成为“静态”(在 C# 意义上)。在 VB 中,在这种情况下,共享就像静态的同义词。