Visual Basic Variables

In Visual Basic, Variables will represent storage locations, and each variable will have a particular data type to determine the type of values the variable can hold.

 

Visual Basic is a Strongly Typed programming language. Before we perform any operation on variables, it’s mandatory to define the variable with a required data type to indicate the type of data the variable can hold in our application.

Syntax of Visual Basic Variables Declaration

Following is the syntax of declaring and initializing variables in visual basic.

 

Dim [Variable Name] As [Data Type]
Dim [Variable Name] As [Data Type] = [Value]

If you observe the above variable declarations, we added a required data type after the variable name to tell the compiler about the type of data the variable can hold.

 

ItemDescription
Dim It is useful to declare and allocate the storage space for one or more variables.
[Variable Name] It’s the variable's name to hold the values in our application.
As The As clause in the declaration statement allows you to define the data type.
[Data Type] t’s a type of data the variable can hold, such as integer, string, decimal, etc.
[Value] Assigning a required value to the variable.

Now, we will see how to declare and initialize the values to the variables in visual basic applications with examples.

Visual Basic Variables Example

Following is the example of using the variables in visual basic.

 

Module Module1
  Sub Main()
   Dim id As Integer
   Dim name As String = "Suresh Dasari"
   Dim percentage As Double = 10.23
   Dim gender As Char = "M"c
   Dim isVerified As Boolean
   id = 10
   isVerified = True
   Console.WriteLine("Id:{0}", id)
   Console.WriteLine("Name:{0}", name)
   Console.WriteLine("Percentage:{0}", percentage)
   Console.WriteLine("Gender:{0}", gender)
   Console.WriteLine("Verfied:{0}", isVerified)
   Console.ReadLine()
  End Sub
End Module

The above example shows that we defined multiple variables with different data types and assigned the values based on our requirements.

 

When we execute the above example, we will get the result as shown below.

 

Id:10
Name:Suresh Dasari
Percentage:10.23
Gender:M
Verfied:True

This is how we can declare and initialize the variables in Visual Basic applications based on our requirements.