Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Dim and Private

Tags:

vb.net

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

like image 551
Akshara Avatar asked Mar 23 '11 05:03

Akshara


People also ask

Is Dim private or public?

Dim defaults to either public or private, depending on what you're working in. A class for example, will default to private.

What is Private in Visual Basic?

Private. The Private keyword in the declaration statement specifies that the element can be accessed only from within the same module, class, or structure.

What is the difference between dim and const keyword?

Simplified answer: Dim is a normal everyday variable. Const doesn't (or shouldn't) be changed. Something that you'll use repeatedly without changing the value.

What is protected in VB net?

The Protected Friend modifier makes a class member accessible from within that class, from derived classes, and from the same assembly in which the class is defined. The Private Protected modifier makes a class member accessible by derived types, but only within its containing assembly.


2 Answers

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.

like image 195
Ando Avatar answered Sep 21 '22 05:09

Ando


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.

like image 27
hallie Avatar answered Sep 22 '22 05:09

hallie