Variables

Getting and setting values

To set the value of a variable you use the following syntax:

VariableName = Value

Where VariableName is the name of the Variable, and Value is the value to be stored. An error will occur if Value is not the correct datatype for that variable.

' General Declarations Section
Dim NumberofEmployees As Integer
' Form Load Procedure
Sub Form1_Load()
    ' Set the variable value
    NumberofEmployees = 150
End Sub

This sets the value of NumberofEmployees to 150 when Form1 Loads

To get the value contained in a variable, you simply put the variablename in place of where you would otherwise type the value:

' General Declarations Section
Dim NumberofEmployees As Integer
' Form Load Procedure
Sub Form1_Load()
    ' Set the variable value
    NumberofEmployees = 150
End Sub
' Command Button1 Procedure
Sub Command1_Click()
    Msgbox "The number of employees is: " & NumberofEmployees
End Sub

The previous code displays a message 'The Number of employees is 150' when Command1 is clicked.

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The most exciting phrase to hear in science, the one that heralds new discoveries, is not 'Eureka!' but 'That's funny...'” - Isaac Asimov