Wednesday, June 15, 2011

Constants in VB.NET

Definition: Constant is a memory location to store data / value. It store one value at a time.


Property: The main property of a constant is that the value stored in it donot changes throughout the program execution.

Declaring Constants:

Const statement is used to declare and allocate memory for one or more variables.  The syntax of declaring variable using Const statement is
[{ Public | Protected | Friend | Protected Friend | Private }]  [Shadows] Const constantname [As datatype] [= expression | initialvalue]

~ Parts of Const Statement ~
  • Public: The constant declared as Public means that you can access data anywhere without any restrictions.
  • Protected: Suppose a constant 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 constant 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 constant that is only accesible in the class where it is declared. You cannot access the Private constant outside the class.
  • Shadows: 
  • constantname: A user defined name of a constant. Multiple constant names must be separated by commas. 
  • datatype: Specifies the type of data stored in a constant. If you do not specify the datatype, the constant 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 constant. in case of expression , the result is assigned as inital value.

No comments:

Post a Comment