Visual Basic 2013 Lesson 9: Working with Variables and Constants in VB2013

[Lesson 8] << [Contents] >> [Lesson 10]

In Visual Basic 2013, data are stored either as variable or as  constant. Variables are similar to mail boxes in a post office. The contents of the variables change from time to time, just like the mail boxes. In  VB2013, variables are areas allocated by the computer memory to store data.

9.1 Variable Names

In Visual Basic 2013 , we have to assign name to every variable. To name a variable in Visual Basic 2013, you have to follow a set of rules.The following are the rules when naming the variables in Visual Basic.

  • It must be less than 255 characters
  • No spacing is allowed
  • It should not begin with a number
  • Period is not allowed.

Examples of valid and invalid variable names are displayed in Table 9.1

Table 9.1:

Valid Names Invalid Name
 My_Mobilephone  My.Mobilephone
Apple123  123Apple
 Long_Name_Can_beUSE  LongName&Canbe&Use                  *& is not acceptable

9.2 Declaring Variables

In VB2013, we declare a variable by assigning name and data type.Variables are usually declared in the general section of the code windows using the Dim statement.
The syntax is as follows:

Dim VariableName As Data Type.

If you intend to declare more variables, you can declare them in separate lines or you can combine them in one line , separating each variable with a comma, as follows:
Dim VariableName1 As DataType1, VariableName2 As DataType2, VariableName3 As DataType3

Example 9.1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LoadDim password As String
Dim yourName As String
Dim firstnum As Integer
Dim secondnum As Integer
Dim total As Integer
Dim doDate As DateEnd Sub

You may also combine the above statements in one line , separating each variable with a comma, as follows:

Dim password As String, yourName As String, firstnum As Integer,………….

Example 9.2

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim YourMessage As String
YourMessage = “Happy Birthday!”
End SubWhen you run the program, a message box that shows the text “Happy Birthday!” will appear, as shown in Figure 9.1


vb2013_figure9.1                                                           Figure 9.1

9.3 Assigning Values to Variables

After declaring variables using the Dim statements, we can then assign values to these variables. The syntax of is


The variable can be a declared variable or a control property value. The expressions include a mathematical expression, a number, a string, a Boolean value (true or false) and more, as illustrated in the following examples:

userName=”John Lyan”
userpass.Text = password
Label1.Visible = True
Command1.Visible = false
Label4.text = textbox1.Text
ThirdNumber = Val(usernum1.Text)
total = firstNumber + secondNumber+ThirdNumber
MeanScore% = SumScores% / NumSubjects%
X=sqr (16)
TrimString= Ltrim (“ Visual Basic”, 4)

In VB 2013, an error occurs when you try to assign a value to a variable of incompatible data type. For example, if you have declared a variable as an integer but you assigned a string value to it, an error occurred, as shown in Example 9.4:

Example 9.3

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim YourMessage As Integer
YourMessage = “Happy Birthday!”
End Sub

When you run the program, the following error messages will appear in a dialog box, as shown in Figure 9.2



Figure 9.2 

You can either break the program and continue to run the program.

9.4 Scope of Declaration

Beside using the Dim keyword to declare the data, we may also use other keywords to declare the data. Three other keywords are private ,static and public. The syntaxes are as shown below:

Private VariableName as Datatype
Static VariableName as Datatype
Public VariableName as Datatype

The aforementioned keywords indicate the scope of declaration. Private declares a local variable, or a variable that is local to a procedure or module. The Static keyword declares a variable that is being used multiple times, even after a procedure has been terminated. Most variables created inside a procedure are discarded by Visual Basic when the procedure is finished, static keyword preserve the value of a variable even after the procedure is terminated. Public is the keyword that declares a global variable, which means it can be used by all the procedures and modules of the whole program.

 9.5 Declaring Constants

A Constant’s value does not change during the running of the program.The syntax to declare a constant is

Const Constant Name As Data Type = Value

Example 9.4

P Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Const Pi As Single = 3.142
Dim R As Single = 10
Dim AreaCircle As Single
AreaCircle = Pi * R ^ 2

MsgBox(“Area of circle with ” & “radius” & R & “=” & AreaCircle)

End Sub

Press F5 to run the program and clicking the button produces the following message:


    Figure 9.3


[Lesson 8] << [Contents] >> [Lesson 10]

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPin on PinterestShare on Tumblr