Variables, Keywords, and Constants in VB .Net

Variable Declaration in VB .Net

Dim Statement in VB .Net

  • The Dim statement is used in the variable declaration and storage/memory allocation for one or more variables.
  • The Dim statement is used at the module, class, structure, procedure, or block level in the program.

Syntax for Variable Declaration in VB.Net :

Syntax: Variable_name[ ( [ boundslist ] ) ] [ As [ New ] datatype ] [ = initializer ]

Where,

  • variablename – is the name of the variable.
  • boundslist – optional. It provides a 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. It specifies the data type of the variable.
  • initializer: Optional if New is not specified. An expression that is evaluated and assigned to the variable when it is created.

Finally, In VB.NET, the declaration of a typical variable uses the following syntax:

Dim Variable_Name As Data_Type

Example:

  • Dim StudentRollno As Integer
  • Dim StudentName As String
  • Dim Salary As Double
  • Dim status As Boolean
  • Dim dt As Date
  • Dim num1, num2 As Integer
  • Dim num1 As Integer
  • Dim num2 As Integer
  • Dim exitButton As New System.Windows.Forms.Button 

Variable Initialization in VB.Net

  • Variables are initialized (assigned with a value) with an equal sign followed by a constant expression. The general form of initialization is:

Dim Variable_Name As Data_Type = Initialization_value(optional)

  • For example –
    • Dim pi As Single = 3.14159
    • Dim CountryName As String = “India”
Dim pi As Single
pi = 3.14159

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.