Wednesday, 19 March 2014

Variable Declaration in VB.Net

The Dim statement is used for variable declaration and storage allocation for one or more variables. The Dim statement is used at module, class, structure, procedure or block level.
Syntax for variable declaration in VB.Net is:

[ < attributelist> ] [ accessmodifier ] [[ Shared ] [ Shadows ] | [ Static ]]
[ ReadOnly ] Dim [ WithEvents ] variablelist

Where,
  • attributelist is a list of attributes that apply to the variable. Optional.
  • accessmodifier defines the access levels of the variables, it has values as - Public, Protected, Friend, Protected Friend and Private. Optional.
  • Shared declares a shared variable, which is not associated with any specific instance of a class or structure, rather available to all the instances of the class or structure. Optional.
  • Shadows indicate that the variable re-declares and hides an identically named element, or set of overloaded elements, in a base class. Optional.
  • Static indicates that the variable will retain its value, even when the after termination of the procedure in which it is declared. Optional.
  • ReadOnly means the variable can be read, but not written. Optional.
  • WithEvents specifies that the variable is used to respond to events raised by the instance assigned to the variable. Optional.
  • Variablelist provides the list of variables declared.
Each variable in the variable list has the following syntax and parts:

variablename[ ( [ boundslist ] ) ] [ As [ New ] datatype ] [ = initializer ]

Where,
  • variablename: is the name of the variable.
  • boundslist: optional. It provides list of bounds of each dimension of an array variable.
  • New: optional. It creates a new instance of the class when the Dim statement runs.
  • datatype: Required if Option Strict is On. It specifies the data type of the variable.
  • initializer: Optional if New is not specified. Expression that is evaluated and assigned to the variable when it is created.
Some valid variable declarations along with their definition are shown here:

Dim StudentID As Integer
Dim StudentName As String
Dim Salary As Double
Dim count1, count2 As Integer
Dim status As Boolean
Dim exitButton As New System.Windows.Forms.Button
Dim lastTime, nextTime As Date
 
Source : http://www.tutorialspoint.com 

No comments:

Post a Comment