vb.net Dim 和 Private 之间的区别

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

Difference between Dim and Private

vb.net

提问by Akshara

What is the difference between Dimand Privatein VB.NET?

DimPrivate在 VB.NET 中有什么区别?

回答by Ando

Dimdeclares and allocates space for a variable. Privateis used to specify an access levelthat means only the declaring class can see or use the declared member.

Dim为变量声明并分配空间。 Private用于指定访问级别,这意味着只有声明类才能看到或使用声明的成员。

I believe your question comes from the fact that you sometimes see things like:

我相信您的问题来自这样一个事实,即您有时会看到以下内容:

Class MyDemoClass
   Dim mVar1 As Integer
   Private mVar2 As Integer
End Class

In the above example mVar1and mVar2declarations are logically equivalent - they both boil down to Private Dim mVar as Integer.

在上面的例子中mVar1mVar2声明在逻辑上是等价的——它们都归结为Private Dim mVar as Integer.

MSDN explains this here:

MSDN在这里解释了这一点

The Dim keyword is optional and usually omitted if you specify any of the following modifiers: Public, Protected, Friend, Protected Friend, Private, Shared, Shadows, Static, ReadOnly, or WithEvents.

Dim 关键字是可选的,如果您指定以下任何修饰符,通常会被省略:Public、Protected、Friend、Protected Friend、Private、Shared、Shadows、Static、ReadOnly 或 WithEvents。

回答by hallie

Dim & Private are two different things. Dim is used to declare variables and allocate memory space. Private is used as access modifier for the variable, on how your variable should be accessed. If you didn't specify an access modifier on a variable it will be Private by default. You can optionally omit Dim by declaring the variable after the access modifier.

Dim 和 Private 是两个不同的东西。Dim 用于声明变量和分配内存空间。Private 用作变量的访问修饰符,关于如何访问您的变量。如果您没有在变量上指定访问修饰符,默认情况下它将是 Private。您可以通过在访问修饰符之后声明变量来选择性地省略 Dim。