6.2 Variables and Constants

 

6.2 Variables and Constants

In the previous section, we have learned about data and various data types. Data can be stored as a variable or as a constant. Variables are like mailboxes in the post office. The content of the variables changes every now and then, just like the mailboxes.

6.2.1 Variable Names

Like the mailboxes, each variable must be given a name. To name a variable in Visual Basic 2022, you must follow a set of rules. The following are eules when naming the variables in Visual Basic:

     It must be less than 255 characters.

     No spacing is allowed.

     It must not begin with a number.

     Period is not permitted.

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

Table 6.4 Variable Name

Valid Names

Invalid Names

My_Computer

My.Computer

 Smartphone123

123Smartphone

 Long_Name_Can_beUSE

 LongName&Canbe&Use    

  *& is not acceptable

 

6.2.2 Declaring Variables

In Visual Basic 2022, we must declare the variables before using them by assigning names and data types. If you fail to do so, the program will show an error. Variables are usually declared in the general section of the code windows using the Dim statement. The syntax is as follows:

Dim VariableName As DataType

If you want to declare more variables, you can declare them in separate lines or combine them in one line, separating each variable with a comma, as follows:

Dim VariableName1 As DataType1, VariableName2 As DataType2, VariableName3 As DataType3

 

Example 6.1 Declaring Variables

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim password As String
Dim yourName As String
Dim firstnum As Integer
Dim secondnum As Integer
Dim total As Integer
Dim doDate As Date
End 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,.............

 

For the string declaration, there are two possible forms, one for the variable-length string and another for the fixed-length string. For the variable-length string, just use the same syntax as Example 6.2.

Example 6.2 Declaring a String Variable

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim YourMessage As String
YourMessage = "Happy Birthday!"
MsgBox(YourMessage)
End Sub

When you run the program, a message box shows the text “Happy Birthday!” will appear, as shown in Figure 6.1

Figure 6.1

For the fixed-length string, you must use the syntax as shown below:

Dim VariableName As String * n

Where n defines the number of characters the string can hold.

Example 6.3 Declaring a Fixed Length String Variable

Dim yourName As String * 10

yourName can hold no more than 10 Characters.

6.2.3 Assigning Values to Variables

After declaring various variables, we can assign values to those variables. The syntax of an assignment is:

Variable=Expression

The variable can be a declared variable or a control property value. The expression could be a mathematical expression, a number, a string, a Boolean value and more, as illustrated in Example 6.4

Example 6.4 Assigning Values to Variables

firstNumber=100
secondNumber=firstNumber-99
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)
Num=Int(Rnd*6)+1

An error will occur 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 6.5.

Example 6.5 Error Assigning a Variable

 

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

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

You can either quit the program or continue to run the program.

 

Figure 6.2

Post a Comment

0 Comments