VB.NET Tutorial III – Hello World!
In this tutorial, we will be making our first hello world! program in both a Console application, and then a Windows Forms application.
In case you don’t know, a console application is a program that is designed to be used on a text based interface such as a text terminal,
command-line interface, or some operating systems. A Windows Forms application refers to a program that is designed for a GUI, in this
case, more specifically for the API (Application Programming Interface) as part of Microsoft .NET Framework.
Now to start creating our own hello world program as a console application in VB.NET: first open the VB.NET IDE, Select File, New,
Project or Solution, then select Console Application or Console Program for VB.NET, and name the project and the output path for
the project, and select Create or OK.
So, once loaded, you should have a program code skeleton like this:
Module HelloWorld Sub Main() End Sub End Module
Here, your whole program is defined between “Module HelloWorld” and “End Module”. A Module for VB.NET is a file (ending in .vb) that
contains program code and sub procedures to execute. You may also include modules into other projects to call on. Now Sub Main (by tradition)
is a sub procedure that is the first sub to be executed, it is where the program starts. A sub is just a block of code that you can call with
parameters which doesn’t return a value. So now inside Sub Main we can place our program code. Let’s start with the manipulation of the
The Console object is an object that refers to the console, that is, the command-line window. Two of the methods for this object is Write
and WriteLine. The Write method prints a string to the console, The WriteLine method prints a string on a new line to the console (which
is equivalent to the ECHO command in cmd.
So if we were to print “hello world!” to the console, we would initiate that statement like this:
and that’s it. Remember, when calling a member the arguments are placed inside parenthesis and each argument value is separated by
a comma as shown above. In this case, there was only one argument; the string “hello world!” for the method WriteLine.
When we run this program, unless we ran it from cmd as the parent process, the application will just print “hello world!” and exit at a
fast rate. So we are going to have to pause the screen, to do that we call the Read method. The Read method simply waits for
the user to press a key and returns the key code for what they pressed. Now our code should look like this:
Module HelloWorld Sub Main() Console.WriteLine("hello world!") Console.Read() End Sub End Module
Now to run it, select the Play button or click Build and the output file should be in the bin folder of the saved path you specified.
After you press any key, the program will exit since the sub has ended and there is no more code to execute.
Now if you want input, use the ReadLine method which reads a string of characters until the user presses Enter.
You may put that data inside a variable like this:
Dim input As String = Console.ReadLine()
Now after the ReadLine method executes, the return value will be assigned to the ‘input’ variable as a string data type.
Then, whenever you include that variable in a value, it will be replaced by the contents of that variable. Example:
Console.WriteLine("What is your name? ") Dim name As String = Console.ReadLine() Console.WriteLine("Hello, " & name)
Noticed I placed the & sign which joins the value of name variable and the string literal “Hello, ” together into one string.
If I just said: “Hello, name” then VB.NET would think that I mean the actual string ‘name’ within the string instead of
it’s value which is why I placed it outside the quotations with an & sign to join the two values.
So let’s say the variable ‘movie’ has a value of “Matrix”, can you figure out what this string value means?
"My favorite movie is the " & movie & " trilogy."
That’s right, the string value is: “My favorite movie is the Matrix trilogy.”
Lastly, after I finish this tutorial part I would like you to notice the Intellisense feature within your .NET IDE. Intellisense is
the window that opens whenever you press a dot or open a parenthesis or even start typing a name within your code.
This feature allows you to explore the objects and members that you are possibly referencing depending on what you
type. The IDE is helping you determine what to code and it helps you learn by predicting what you will type.
In intellisense, you will see icons next to the names of items. Methods are represented by pink flying erasers and
properties are represented by tape cassettes with a hand on it. I have always associated methods and properties
with flying erasers and tape cassettes, haven’t you?