Definition: Variable is a memory location to store data / value. It store one value at a time.
Property: The main property of a variable is that the value stored in it changes throughout the program execution.
Declaring Variables
Dim statement is used to declare and allocate memory for one or more variables. The syntax of declaring variable using Dim statement is
[{ Public | Protected | Friend | Protected Friend | Private | Static}] [Shared] [Shadows] [ReadOnly] Dim [WithEvents] variablename[(boundlist)] [As [New] datatype] [= expression | initialvalue]
~ Parts of Dim Statement ~
- Public: The variable declared as Public means that you can access data anywhere without any restrictions.
- Protected: Suppose a variable is declare inside the class and you do not want it to be access outside the class then protected access specifier allows you to declare a variable that is only accessible in the class where it is declared and its derived class.
- Protected Friend:
- Private: Private access specifier allows you to declare a variable that is only accesible in the class where it is declared. You cannot access the Private variable outside the class.
- Static: When the procedure or function ends then it automatically releases the memory allocated to local variables. As a result, the value stored in a local variables is also lost. If you want to retain the value of local variables after procedure or function ends than that variable must be declared as Static.
- Shared: Every instance of a class keeps its own copy of variable (separate memory allocation and store different values). When a variable is declared as Shared, all instances access the same memory location and is available to every instance of class. If one instance changes value of a shared variable, all other instances access the new updated value.
- Shadows:
- ReadOnly: It means you can only read the value of a variable. It is not possible to write anything.
- WithEvents: It means that variable can respond to an event.
- variablename: A user defined name of a variable. Multiple variable names must be separated by commas and the datatype of all the variables is given in the first As clause.
- boundlist: It is used to declare arrays. An array can have upto 60 dimensions.
- New: Creates a new instance of the class when statement executes.
- datatype: Specifies the type of data stored in a variable. If you do not specify the datatype, the variable takes the datatype of expression | initialvalue. If you do not specify datatype and expression, the datatype is set to Object.
- expression | initialvalue: Used to initialize a variable. in case of expression , the result is assigned as inital value.
No comments:
Post a Comment