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 so before we perform any operation on variables, it’s mandatory to define the variable with a required data type to indicate that the type of data the variable can hold in our application.
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.
|Dim||It is useful to declare and allocate the storage space for one or more variables.|
|[Variable Name]||It’s the name of the variable 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.
Following is the example of using the variables in visual basic.
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
If you observe the above example, 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.
This is how we can declare and initialize the variables in Visual Basic applications based on our requirements.