VB.Net Tutorial

VB.Net Tutorial #1

Once in a while, something happens in the computer industry happens that simply cannot be ignored and when it is created by the biggest software company in the world, everyone has to sit up and take notice. The .NET (pronounced DOT NET) initiative grew out of work being done in the Visual J++ department which had created a set of libraries called WFC (Windows Foundation Classes) allowing Java applications to be compiled for the Windows (and only the Windows) platform. This got up the noses of the creators of Java and so it fizzled out. Then there came the .NET platform created from the WFC. And with came a new version of Visual Basic. Why not stick with VB6? Because there is no guarantee that the new version of Windows (whatever it happens to be called) will run it properly. So what is all the fuss about? Our first VB.Net program:-

Imports System

Class hello

	' This is the usual Hello World! program
	Public Shared Sub Main()
		Console.WriteLine("Hello World!")
	End Sub
End Class 

Doesn't look to bad does it? Compare this with a Java program that does the same thing:-

using System;

class hello
{
	// This is the usual Hello World! program

	public static void Main()
	{
		Console.WriteLine("Hello World!");
	}
}

It should be noted that output is to the text console and doesn't produce a GUI window (thats for later). So now you have to compile it and for that you will need a compiler. Now, you're probably getting a bit worried that this is going to cost you money. I mean, surely Microsoft wouldn't give it away for free! Well, actually it seems that they do. The bit you have to pay for is the IDE (Integrated Development Environment) which makes life easier but actually isn't required to write modest sized programs. So where is it? If you have installed version 1.1 of the dotnet framework from your Windows XP CD then you've already got all the compilers you need to follow these tutorials (Win98 users can download the dotnetfx file and install it to run the .NET framework on Win98 and hence use these tutorials. You will also need MDAC 2.7 which is available as a download from the same page but you may already have it if you have been using VB6. You CANNOT run Visual Studio.Net on Win98). You now open a Command Console (in the WinXP Accessories menu) and type:-

path=c:\windows\microsoft.net\framework\v1.1.4322\
dir *.exe

Your version number may be a bit different but the above is correct for my machine running SP2. The dir command will list all the files and amongst it all you will see the following files:-

jsc.exe
vjc.exe
csc.exe
vbc.exe

This opens up some very interesting possibilities. Not only is there a C# compiler (csc.exe) but a Visual J# vjc.exe (the successor to J++), a Visual Basic Compiler (vbc.exe) and finally a Javascript compiler (jsc.exe). Does this mean I can write .NET applications in Javascript? Apparently so although I haven't had much chance to play with it (I can feel another set of tutorials coming on but not yet). And it hasn't cost you a penny! So how to use this compiler? Create a batch file (called cc.bat) and fill it with the following commands:-

cd\
cd csc
path=c:\windows\microsoft.net\framework\v1.1.4322\
vbc %1.vb

Then create a folder called vbc and put the batch file in it. Create another file called hello.vb and put the above VB code in it. Save and run from the command line:-

cc hello

and press the return key. It compiles and deposits a hello.exe in the vbc folder. Now just run the hello.exe by typing:-

hello

and the famous words will be displayed. Congratulations, you've created your very first VB.Net program! It isn't very exciting I admit but its a start.

To summarise the steps.

Now we'll go through the code a line at a time. Bear with me, it is relevent and will help you understand what is to follow when we get to the more interesting stuff.

Imports System;

This is telling the compiler to include the system library which it needs to compile a console program. Note that there is a semicolon on the end - this tells the compiler that this is the end of a statement.

 Class hello

This starts a class which is a form of subroutine if you are familar with C# or C++ although there is more to it than that but will suffice as an explanation for the moment. Not that we don't need any curly brackets in VB!

' This is the usual Hello World! program

This is a comment again with a apostrophe at the begining of the line show that the whole line is a commant and there is no need for a terminating character (apart form cr/lf).

	Public Shared Sub Main()

The familiar Main function beloved of C programmers makes it to VB (although its just a subroutine) and is the beginning of the program. Now we actually do something:-

		Console.WriteLine("Hello World!")
	End Sub
End Class 

This writes to the console window and the end sub closes the main program and the end class ends the program itself. And that's all there is to it. Well, alright there is much work to be done to get a GUI window displaying but I'll take you gently through the syntax of the language using console programs and work our way to the GUI stuff.

Back to VB.Net Tutor

Written by Nick Cheesman. Last updated: 01/06/2007
Please eMail me at:
nickjc@nickjc.co.uk