vb.net VB中“公共共享”子/函数的含义

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

Implications of "Public Shared" Sub / Function in VB

vb.netvb6-migration

提问by osdTech

Can anyone explain me in VB i need to use Public Shared Sub so it can be accessed from another form.

任何人都可以在 VB 中解释我我需要使用 Public Shared Sub 以便可以从另一种形式访问它。

But what this "Public" and "Shared" means?

但是这个“公共”和“共享”是什么意思?

  • Who is public?
  • With who is shared?
  • 谁是公众人物?
  • 与谁共享

If it is public shareddoes this means some other software or "some hacker app" can easier have access to this sub and it's values?

如果它是公共共享的,这是否意味着其他软件或“某些黑客应用程序”可以更轻松地访问此子程序及其值?

采纳答案by dwerner

In VB.NET, Sharedis equivalent to staticin C# - meaning the member belongs to the class, not an instance of it. You might think that this member is 'Shared' among all instances, but this is not technically correct, even though VB.NET will resolve a Sharedmember though an instance invocation.

在 VB.NET 中,Shared相当于static在 C# 中 - 意味着成员属于类,而不是它的实例。您可能认为该成员在所有实例之间是“共享的”,但这在技术上是不正确的,即使 VB.NET 将Shared通过实例调用解析成员。

public class1
    public shared something as string
    public somethingelse as string
end class

The following code illustrates how VB.Net allows you to access these:

以下代码说明了 VB.Net 如何允许您访问这些:

...
class1.something = "something" 'belongs to the class, no instance needed

dim x as new class1() with {.somethingelse = "something else"}

Console.WriteLine(x.somethingelse) 'prints "something else"

Console.Writeline(class1.something) 'prints "something" <- this is the correct way to access it

Console.Writeline(x.something) 'prints "something" but this is not recommended!
...

Publicmeans any linking assembly can see and use this member.

Public意味着任何链接程序集都可以看到和使用这个成员。

回答by RBarryYoung

The Publicaccessor keyword simply means that the method, property, etc. is visible and callable from outside of the DLL or Assembly that defined it.

Public存取器简单地keyword意味着这个方法,属性等是从定义它的DLL或组件外部可见和可调用。

The Sharedkeyword means that the method, etc. is not "instanced". That is, it is part of the Classdefinition only, and notpart of the objects that are created ("instanced") from that Class definition. This has two principal effects:

Shared关键字意味着,该方法等是不是“实例化”。也就是说,它只是定义的一部分,而不是从该类定义创建(“实例化”)的对象的一部分。这有两个主要影响:

  1. The Shared method can be called at anytime, without actually having an object/instance of that Class. and,
  2. Shared methods cannot access any of the non-Shared parts of the Class definition (unless an object instance is passed to it). They can only directly access the other Shared parts of the Class definition.
  1. Shared 方法可以随时调用,而实际上没有该类的对象/实例。和,
  2. 共享方法不能访问类定义的任何非共享部分(除非将对象实例传递给它)。它们只能直接访问类定义的其他共享部分。