vb.net VB.NET中共享变量有什么用?

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

What is the use of a shared variable in VB.NET?

vb.net

提问by

What is the use of a Sharedvariable in VB.NET?

SharedVB.NET中的变量有什么用?

采纳答案by MrTelly

It is the same as staticin C# and most other languages. It means that every object in the class uses the same copy of the variable, property or method. When used with a method as it is static you don't need an object instance.

它与staticC# 和大多数其他语言中的相同。这意味着类中的每个对象都使用变量、属性或方法的相同副本。当与静态方法一起使用时,您不需要对象实例。

MyClass.DoSomething()

rather than

而不是

Dim oObject as New MyClass()
oObject.DoSomething()

回答by CraigTP

The "Shared" keyword in VB.NET is the equivalent of the "static" keyword in C#.

VB.NET 中的“Shared”关键字相当于 C# 中的“static”关键字。

In VB.NET, the Shared keyword can be applied to Dim, Event, Function, Operator, Property, and Sub statements within a class; however, in C#, the statickeyword can be applied both to these statements within a normal class, and also at the class level to make the entire class static.

在 VB.NET 中,Shared 关键字可以应用于类内的 Dim、Event、Function、Operator、Property 和 Sub 语句;但是,在 C# 中,static关键字既可以应用于普通类中的这些语句,也可以应用于类级别以使整个类成为静态。

A "Shared" or "static" method acts upon the "type" (that is, the class) rather than acting upon an instanceof the type/class. Since Sharedmethods (or variables) act upon the type rather than an instance, there can only ever be one "copy" of the variable or method as opposed to many copies (one for each instance) in the case of non-shared (i.e., instance) methods or variables.

“共享”或“静态”方法作用于“类型”(即类),而不是作用于类型/类的实例。由于Shared方法(或变量)作用于类型而不是实例,因此在非共享(即,实例)方法或变量。

For example: If you have a class, let's call it MyClass with a single non-shared method called MyMethod.

例如:如果您有一个类,让我们将其称为 MyClass,并使用一个名为 MyMethod 的非共享方法。

Public Class MyClass
    Public Sub MyMethod()
        ' Do something in the method
    End Sub
End Class

In order to call that method you would need an instance of the class in order to call the method. Something like:

为了调用该方法,您需要一个类的实例来调用该方法。就像是:

Dim myvar As MyClass = New MyClass()
myvar.MyMethod()

If this method was then made into a "shared" method (by adding the "Shared" qualifier on the method definition, you no longer need an instance of the class to call the method.

如果此方法随后成为“共享”方法(通过在方法定义上添加“共享”限定符,则不再需要类的实例来调用该方法。

Public Class MyClass
    Public Shared Sub MyMethod()
        ' Do something in the method
    End Sub
End Class

And then:

进而:

MyClass.MyMethod()

You can also see examples of this in the .NET framework itself. For example, the "string" type has many static/shared methods. I.e.

您还可以在 .NET 框架本身中查看此类示例。例如,“字符串”类型有许多静态/共享方法。IE

' Using an instance method (i.e. Non-shared) of the string type/class.
Dim s As String = "hello"
s.Replace("h", "j")

' Using a static/shared method of the string type/class.
s = String.Concat(s, " there!");

Here's a good article that explains it further:

这是一篇很好的文章,进一步解释了它:

Shared Members and Instance Members in VB.NET

VB.NET 中的共享成员和实例成员

回答by Bartek Szabat

Simply whenever you want to have single instance of variable for entire application, shared between objects of your class. Instead of 1-per-object.

只要您想为整个应用程序拥有单个变量实例,就可以在类的对象之间共享。而不是每个对象 1 个。