Difference between Dim and Private

前端 未结 2 1445
难免孤独
难免孤独 2020-12-14 15:34

What is the difference between Dim and Private in VB.NET?

相关标签:
2条回答
  • 2020-12-14 15:52

    Dim declares and allocates space for a variable. Private is used to specify an access level that means only the declaring class can see or use the declared member.

    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 mVar1 and mVar2 declarations are logically equivalent - they both boil down to Private Dim mVar as Integer.

    MSDN explains this here:

    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.

    0 讨论(0)
  • 2020-12-14 16:03

    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.

    0 讨论(0)
提交回复
热议问题