VB.NET Tutorial II – Variables and Data Types

VB.NET has certain ways of organizing and handling data. Let’s start with data types. Data types are nothing more than a set of values, when you define a data type you are also defining what kind of operations are to performed on that data. Examples of common data types are integers, strings, characters, booleans, and much more.

Let’s start with the Integer data type. An integer is a number, with numbers you are able to perform mathematical operations on them such as increasing the number’s value, decreasing, multiplying, dividing, etc.

The Character data type (char) is a single byte value that represents it’s graphical ascii symbol depending on its value. Characters include letters, numerical digits, punctuation marks, funny symbols, and even white space. For example, how many characters are in this sentence?: “Hello World!”, there are 13 characters, this includes all the letters, the exclamation mark, the space, and the NULL terminator character (charcode 00). In VB.NET when specifying characters, you do so by


where # is the character code number of the character. For example: chr(33) represents the ! character, chr(48) represents the character 0, and chr(65) means the letter A.

Strings are a sequence of characters, with strings you are able to perform certain operations such as search for a string or character within a string, add or remove characters or strings, get substrings, and maybe more. When specifying strings values in VB you surround the string with double quotation marks: “This is a string”.

Booleans are by far the simplest data type, as they can only represent one of two values, true or false. Booleans are mostly used as flags or bit imitation.

One important thing to remember is that you cannot treat data types like another different data type. You can have an integer value set to 2+3 but not “2”+”3″, because it doesn’t make since for the string literal values of “2” and “3” to yield a numerical value. Just keep in mind that 2, “2”, and chr(2), are different because of their data types.

When declaring variables in VB.NET, it is encouraged that you declare the data type of the variable. The syntax of declaring a variable in VB.NET is:


After that, you could optionally set a value for that variable.
So, this:

Dim msg As String = "hello world"

is the same as:

Dim msg As String
msg = "hello world"

Where we are setting the string value of the string data type variable ‘msg’ as “hello world”.

Here I will declare more variables in this example:

Dim name As String = "Yu-Gi-Oh! Structure Deck - Machina Mayhem"
Dim price As Integer
Dim TCG-Release As Boolean = False
price = 12

Now, with variables you can set the value of a variable equal to the value of another variable:

Dim filename As String = "C:\foo.txt"
Dim copy As String = filename

This is completely legal because the declared ‘copy’ variable is of type String, and since the variable ‘filename’ is of the string data type, it’s value can be or be part of the value of ‘copy’.

But obviously you cannot have two different datatypes have their values exchanged; this is not legal:

Dim i As Integer
Dim s As String = "asdf"
i = s

With logic, you can of course spot the error that “asdf” is not an integer value so it cannot be a value of i.

Now in VB.NET, when assigning values to variables, you can do more than just directly assign a new value. You can also append a value to the start or end of the original value of that variable or other things depending on the data type.

You may use the &= operator when assigning values instead of the = sign if you wish to append a new value to the end of the current value of the variable:

Dim a As String = "abc"
a &= "def"

‘var &= val’ is the same as ‘var = var & val’, so in this case the value of the variable ‘a’ now is “abcdef”. This &= assignment operation could be compared to this in batch:

aet a=abc
set a=%a%def

Now there are a number of assignment operators for numerical data types. Take a look at this:

+= Increment variable by given value
-= Decrement variable by given value
*= Multiply variable by given value
/= Divide variable by given value
^= Raise variable power by given value

So If a=2 then after a+=3, a would equal 5. Likewise if a=5, b=4, and c=3 then after a+=b+c, a would equal 12.

That’s all for now, next time I’ll talk about data structures.

One thought on “VB.NET Tutorial II – Variables and Data Types

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: