Tuesday, 28 August 2012

VB.NET Programming lesson #2 - Your First Program


The first thing you need to do is create a source file. A source file is a text file that contains the source code or in other words the commands that the program must run. VB.NET source files usually have the file extension .vb. We are going to be using Notepad to write our VB.NET code.

Create a text file called hello.vb somewhere where you can easily find it. I am going to create a folder calledvbnet in the root of my C drive and then create the source file in there which might be a good idea for you too. Make sure you have the file open in Notepad and then add the lines of code that follow to your source file. Each line of code is explained as I go along.

Imports Microsoft.VisualBasic
Imports System

The Imports keyword is used to import namespaces. This means that you can use the commands in that namespace. The Microsoft.VisualBasic namespace contains commands that you will be using very often that are specific to Visual Basic.NET. The System namespace contains a lot of useful commands and classes, one of which is the command to write things on the screen which you will see later. There are lots of other namespaces to import but the two in the above example are the most important.

Class Hello

The above line containts the Class keyword which is used to define a class. It is followed by the name of the class which in this case is Hello. A class is a program module and most of the time you need to put your programs in classes but you will find out why in a later lesson.

Shared Sub Main()

This line declares the main subprocedure. A subprocedure is a block of code and is used to group parts of a program together. The Shared keyword is something you don't have to worry about now but what it does is allow the subprocedure to be run without instantiating the class. The Sub keyword is used to show that you are defining a subprocedure. Main is the name of the subprocedure and it is where the program starts executing commands from. The two brackets after Main are used for containing parameters but you don't have to worry about that right now.

Console.WriteLine("Hello")

The Console.WriteLine command writes text on the screen. It is followed by brackets which contain the text to write on the screen. All types of text must be put inside double quotes like in the example above because this helps to make sure that it is interpreted by Visual Basic.NET as text instead of as a command.

Console.ReadKey()

Console.ReadKey is a command that waits for the user to press a key before carrying on with the program. If you want to double click on the .exe file of your program to run it instead of running it from a command prompt then you will need this command because the console window immediately disappears after writing Hello if you don't add it in.

End Sub

This line is used to end a subprocedure which in this case is the Main subprocedure. Every time you declare a subprocedure you must remember to use End Sub to end it.

End Class

End Class works in the same way as End Sub but is used for ending a class. Every class must be ended with it.

That is all the code for your first program. Here is what the whole program should look like.

Imports Microsoft.VisualBasic
Imports System

Class Hello

   Shared Sub Main()
      Console.WriteLine("Hello")
      Console.ReadKey()
   End Sub

End Class

You will notice that I have moved some parts of the program a little bit to the right. This is called indenting and is used to make it easier to see the structure of your code. Indenting your code will make it much easier to read and write but you will only start appreciating how important it is when you start writing bigger programs. I have also used empty lines in some places to make the code easier to read. All of this is standard practice so you should get used to it now rather than later.

Compiling the program

To be able to run a program you first need to compile it. When you compile a program, you use a program called a compiler which reads your source code and produces an executable file with a .exe extension. Your source file is a human readable version of what your program should do and the executable is the machine readable program that is actually run by the computer.

To compile your program you need to make sure that you have followed the instructions in the previous lesson. If you have then you will have the .NET Framework SDK installed which includes a Visual Basic.NET compiler called vbc.exe.

VERY IMPORTANT
You will not be able to use the vbc command unless the .NET Framework SDK directory is in your system path. You might find a program on your start menu after installing the .NET Framework SDK that runs a command prompt with the path set properly. If you are unable to find it then your need to add it to your path by yourself. This is a very complex process which can be done in many different ways depending on what operating system you have and what version of the .NET Framework SDK you have. One easy way to get around the problem is to create a .bat file like the following one and run it when you want to compile programs using vbc.

@ECHO OFF
ECHO VB.NET Programming Environment
@SET PATH=%PATH%;C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
ECHO PATH=%PATH%
CD \vbnet
%ComSpec%

Save the bat file as vbprgenv.bat in your vbnet folder that you created earlier. You might need to change some things in the .bat file to get it to work if you aren't using the 2.0 version of the .NET Framework but in most cases it should work.

You now need to open a command prompt window or run the .bat file from above depending on how you have chosen to do it. Change to the directory in which you saved your source file if you are not already in it. Now make sure that you have saved the source file you created because the compiler can't compile a file that hasn't been saved. Type the following command and press Enter to compile the program.

vbc hello.vb

If you have done everything correctly then it will compile the program without printing any error messages. If you see error messages then you need to go back through the instructions and figure out what you have done wrong. If the program compiled succesfully then an executable file called hello.exe will be created in the same directory as your source file. You can run it be either double clicking on it or by typing in its name on the command prompt and pressing Enter. The program should print Hello on the screen and then wait for you to press a key.
Congratulations on making your first program in Visual Basic.NET.

Using comments

A comment is something you put in a program that is ignored by the compiler but can be used by you to remind yourself of what a part of a program does or just for anything that must not be compiled into the program. You can add a comment to a program using a single quote. The text that you want in the comment must be put after the single quote. Here is an example of how to do it with the comments in bold.

' This is my first program in Visual Basic.NET

Imports Microsoft.VisualBasic
Imports System

Class Hello

   Shared Sub Main()
      Console.WriteLine("Hello") ' Writes Hello on the screen
      Console.ReadKey() ' Waits for the user to press a key
   End Sub

End Class

Practice

To practise what you have learnt, try writing a program that writes things on the screen on multiple lines. After doing that write a program that draws a small tree on the screen using asterisks (*) and use spaces to get the asterisks in the right position.

0 comments:

Post a Comment