vb.net 共享和静态之间有什么区别?

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

What are the differences between Shared and Static?

c#vb.netstaticstatic-methodsshared

提问by sab669

I'm a C# developer but I've inherited a legacy VB app today with 0 documentation what so ever. I've been starting to read through the code and reference the list of VB keywordsevery 5 seconds.

我是一名 C# 开发人员,但我今天继承了一个遗留的 VB 应用程序,其中有 0 个文档。我已经开始通读代码并每 5 秒引用一次VB 关键字列表

I guess I don't understand the distinction between Sharedand Static.

我想我不明白之间的区别SharedStatic

Reading this post: https://stackoverflow.com/a/1980293/1189566

阅读这篇文章:https: //stackoverflow.com/a/1980293/1189566

It states:

它指出:

VB doesn't have static, it has shared

VB 没有static,它有shared

But you can see in the list of keywords linked above, Staticis a reserved keyword. It looks like Staticis only applicable to fields, where as Sharedcan be on a method ora field?

但是你可以在上面链接的关键字列表中看到,Static是一个保留关键字。看起来Static只适用于字段,哪里Shared可以用于方法字段?

I guess ultimately I'm just hoping someone could expand upon the answer I linked to provide some more details for a VB noob.

我想最终我只是希望有人可以扩展我链接的答案,为 VB 菜鸟提供更多详细信息。

For example, say I had this

例如,假设我有这个

public class MyClass
    Dim myVar as Integer = 1

    public shared sub UpdateMyVar()
        myVar = 2
    end sub
end class

public class MyOtherClass
    Dim cOne = New MyClass()
    Dim cTwo = New MyClass()

    cOne.UpdateMyVar()
    txtMyTextBox.Text = cTwo.myVar.ToString()
end class

Please forgive any syntactical issues. Assume this code compiles. I've literally just started skimming the code an hour and a half ago.

请原谅任何语法问题。假设这段代码可以编译。从字面上看,我一个半小时前才开始浏览代码。

Would cTwo.myVarbe 1or 2? I'm guessing 2since Sharedseems to affect all instances of a class? That seems tremendously dangerous.

cTwo.myVar1还是2?我猜2因为Shared似乎影响了一个类的所有实例?这似乎非常危险。

回答by Matt Wilko

The equivalent of the C# Staticmethodmodifier is Sharedin VB.net

C#Static方法修饰符的等价物Shared在 VB.net 中

The closest equivalent of the C# Staticclassmodifier in VB.Net is a Module

VB.Net中与 C#Static修饰符最接近的等价物是Module

The Statickeyword in VB.NET defines a local variable that exists for the lifetime of the process. There is no equivalent of this in C#.

StaticVB.NET 中的关键字定义了一个在进程的生命周期内存在的局部变量。在 C# 中没有这样的等价物。

For a great reference of comparison between the two see this link: https://www.harding.edu/fmccown/vbnet_csharp_comparison.html

有关两者之间比较的重要参考,请参阅此链接:https: //www.harding.edu/fmccown/vbnet_csharp_comparison.html

回答by Wade73

For VB.Net you use shared exactly like Static is used in C#, but VB.Net has a static keyword as well and it is used to retain a variable value even after the method call has ended. So the next time you call a method it will have that previous value. MSDN has a more detailed explanation here - http://msdn.microsoft.com/en-us/library/z2cty7t8.aspx

对于 VB.Net,您使用 shared 与 C# 中使用的 Static 完全一样,但 VB.Net 也有一个 static 关键字,即使在方法调用结束后,它也用于保留变量值。因此,下次您调用方法时,它将具有先前的值。MSDN 这里有更详细的解释 - http://msdn.microsoft.com/en-us/library/z2cty7t8.aspx

From the link there are some interesting behaviors:

从链接中有一些有趣的行为:

When you declare a static variable in a Shared procedure, only one copy of the static variable is available for the whole application. You call a Shared procedure by using the class name, not a variable that points to an instance of the class.

When you declare a static variable in a procedure that isn't Shared, only one copy of the variable is available for each instance of the class. You call a non-shared procedure by using a variable that points to a specific instance of the class.

在共享过程中声明静态变量时,整个应用程序只能使用静态变量的一个副本。您可以使用类名调用共享过程,而不是使用指向类实例的变量。

当您在非 Shared 的过程中声明静态变量时,该类的每个实例只能使用该变量的一个副本。您可以使用指向类的特定实例的变量来调用非共享过程。