Declaring variables in Visual Basic 2015 and its uses

Asked By 30 points N/A Posted on -
qa-featured

Hello!

I would like to know what is/are variables. How to declare it in Visual Basic 2015? Can you show me a sample or way how to declare the variables? What is dim? Why do we need to declare local variables and global variables? What is/are differences between local variable and global variables? What are the purpose of it? How useful is it in computer programming? Can you give me ways on declaring different data types using variable declaration?

Thank you!

SHARE
Answered By 0 points N/A #173151

Declaring variables in Visual Basic 2015 and its uses

qa-featured

Hello Pamela!

Variable is a location or space in the computer’s memory where you can store values temporarily. We can use letters or words to define a variable. To declare a variable please follow the format below.

Dim memory1 as Integer

Explanation: where “memory1” is the variable

“Dim” means “Declare in Memory” in Visual basic. We have to use this to inform the computer that we are establishing a variable or object.

Local variable – can be only used in a local control

How to declare local variable

Private Sub cmdAdd_Click()

Dim sum as integer

End Sub

Private Sub cmdSub_Click()

Dim difference1 as integer

End Sub

Explanation: “sum” is a variable declared only inside cmdAdd_click() and cannot be used by any functions it cannot be used to cmdSub_Click()

Global variable – can be used by multiple controls

Here is the sample declaration.

Option Explicit

Dim A as integer = 3

Private Sub cmdAdd_Click()

Dim sum as integer

sum = A+1

End Sub

Private Sub cmdSub_Click()

Dim difference1 as integer

Difference1=A-1

End Sub

Explanation: above is the illustration of declaring global variable. Where “A” is a global variable that can be used for both command buttons.

Declaring variables is very important to properly addresses a value in computer. Declaring global variables is important to save space, time, and redundancy in declarations. The lesser declaration, the smaller the program, the better output.

Related Questions