![]() |
VB.Net Tutorial #2
Welcome to the second VB.Net tutorial. It is my intention in these tutorials to offer example programs that demonstrate various aspects of the Visual Basic.Net language but using as little code as possible. GUI programming by its very nature is verbose and can sometimes hide what are very simple concepts at heart so I shall be sticking with console applications for the time being.
You will need to be running Windows XP with versiion 1.1 of the .Net framework installed (its on the WinXP CD) with Service Pack 2 also installed (you shouldn't be running XP without it!). Your only other application will be notepad and that's it! No ned to buy expensive Integrated Development Environments (IDEs) although it will help once we get into GUI programming but more on that later. On with our next example:-
Imports System Class hello Public Shared Sub Main() Dim i As Integer Dim k As Char Dim hello As String Dim d As Double i = 1 k = "a" hello = "hello" d = 1.23456789 Console.WriteLine(i) Console.WriteLine(k) Console.WriteLine(hello) Console.WriteLine(d) i = 2 k = "b" hello = "bye" d = 3.23456789 Console.WriteLine(i) Console.WriteLine(k) Console.WriteLine(d) Console.WriteLine(hello) End Sub End Class
Don't worry that it looks a bit long - it is really just repeating the same thing over and over again. Here we are interested in what are called variables. They are variables because what they contain can vary. Variables come in many guises (there are more but these four will do for the moment). First there is an integer and is the first statement after main(). An integer can contain numbers without a decimal point and in this case we have called our variable with the single letter i so
Dim i as Integer
means that we have created (dimensioned) a variable that will contain a number without a decimal point but as yet it doesn't contain anything. Now it is important to ensure that ALL variables are initialised with a value of some kind as it might contain garbage (this isn't quite such a problem with modern compilers but it is good practise to assign a value anyway). So if you look further down the program, we see this:-
i = 1
which assigns a value of 1 to the variable. Further down the program we find:-
Console.WriteLine(i)
which will write the value of i (which is currently set to 1) to the console just to prove that it is doing what we expect. The next block has the integer value of i changed to 2 - this isn't being added to the variable, it is replacing what was there before and the next block displays the new value.
Going back to the top of the program, there are other variables that are being created, assigned, displayed, changed and displayed again. These are:-
Dim k As char
This holds a single letter of the alphabet which can letters A-Z or a-z or any of the other characters available on the keyboard. This could confusingly include numbers 0-9 but it would store it as a letter NOT a number so we couldn't use this variable to perform mathematics on (such as adding to it). Because it is a character, the assignment has to be surrounded by double quotes as in:-
k="a"
Another type of variable is a string:-
Dim hello As String
It should be noted that variable names can be single letter or use more that one as shown here with the varibale name called hello. It is usually wise to create meaningful variable names so you use a name that gives you an idea what it can contain. This is a string of characters so it is assigned as follows:-
hello="hello";
The variable name is used in exactly the same way in the writeline statements as all the others. Finally there is the double which is a number but it can contain a decimal point as shown. Compile the program and watch how the variable contents change. This is a completely new program so create a new file (vars.vb for example) and compile it with the new name which will create a new executable program with this name.
Now here is a little homework for you. Try changing the contents of a variable by adding something to it. For example, to add 1 to the variable i we would use the statement:-
i=i+1
and use the writeline statement to verify that it does actually change.
There is another way of writing variable creation and assigment. Heres another new program (vars2.vb perhaps) which illustrates this:-
Imports System Class hello Public Shared Sub Main() Dim i As Integer = 1 Dim k As Char = "a" Dim hello As String = "hello" Dim d As Double = 1.23456789 Console.WriteLine(i) Console.WriteLine(k) Console.WriteLine(hello) Console.WriteLine(d) i = 2 k = "b" hello = "bye" d = 2.23456789 Console.WriteLine(i) Console.WriteLine(k) Console.WriteLine(hello) Console.WriteLine(d) End Sub End Class
It shows that a variable can be created and assigned a value in one statement as in:-
Dim i As Integer = 1
This takes up less room and is a little more pleasing to the eye. Once again, the second block of assignments is the same as before because you cannot create variables with the same name in the same program. I think that should give you something to think about ready for when I see you again for the next tutorial.