Declaring variables

In order to make you program easy to understand, and therefore easier to de-bug, it is a good idea to declare all your variables. Although VB does not force you it is a good habit to get into. Firstly it will save memory, by explicitly saying how big the variable will be, and also, if you ever use other languages like C++ you are not given the option of not declaring your variables. To declare a variable you use the following syntax:

Scope  VariableName As DataType

Scope represents the range of the variable. VariableName is a string representing the name of the variable. DataType represents what sort of data is stored in the variable. If DataType is not specified, then it is set to Variant. For example,

Public NumberOfEmployees As Integer

if declared in the General Declarations section of a form, NumberOfEmployees can be  accessed and changed in all procedures in that form. NumberOfEmployees can only store an Integer.

You might also like...


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.


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.

“A computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are, in short, a perfect match” - Bill Bryson