Get up to 80 % extra points for free! More info:

Lesson 2 - Visual Studio and your first VB.NET console application

Lesson highlights

Are you looking for a quick reference on creating a VB.NET project in Visual Studio instead of a thorough-full lesson? Here it is:

  • Download Visual Studio Community Edition at https://www.visualstudio.com/…nity-vs.aspx
  • Install Visual Studio and check to install .NET desktop development kit, choose Visual Basic (VB.NET) if needed
  • Run Visual Studio and select File -> New -> Project in the application menu:
Creating a new project in Visual Studio - Visual Basic (VB.NET) Basic Constructs
  • Select the Visual Basic template -> Windows and in the next menu select Console Application:
Creating a new console application in VB.NET - Visual Basic (VB.NET) Basic Constructs
  • Edit the project name and location if needed and confirm the form
  • Hit the green play button in the toolbar to run the project.

Would you like to learn more? A complete lesson on this topic follows.

In the previous lesson, Introduction to Visual Basic .NET and the .NET framework, we talked about the language itself and went over what the .NET framework is. In today's lesson, we're going to focus on the Visual Studio IDE. We'll show you how to use it and program a simple console application.

IDE stands for Integrated Development Environment. In a nutshell, it's an application in which we'll write a source code, and then use it to run, test and debug our application.

Of course, we'll have to start by installing Visual Studio. If you have access to paid versions, which come with advanced features, go ahead and use it, but the free Community version will be more than enough for us. You can download the Visual Studio 2015 Community Edition on the "official Microsoft site": https://www.visualstudio.com/…nity-vs.aspx.

Installation

Even if English isn't your native language, I highly suggest installing the English version of Visual Studio. The advantage to using English applications is that if you run into problems, you will most likely not be able to find the answers in your language (e.g. with the advanced configuration of databases or websites). All of your applications and programs should be also written in English, but for the start feel free to use your native language without using accent characters, if your language has any. If you want to become a professional programmer, you should try to improve your language skills, and write everything in English.

The installation will ask about the preferred language, choose Visual Basic .NET and continue by clicking the next button :). After installation, you may need to register your Visual Studio. Registration is free, after which you'll receive a serial number that allows you to use this program legally.

Backup and version control

Programmers usually need a tool that will provide version control and backup of his work. We can't rely on the fact that we could just save the program because we're humans and humans make mistakes. When you lose a few days' or even a few weeks' work, it can be really demotivating. It's good to think about such situations right from the start. I highly recommend Dropbox, which is extremely simple, and automatically stores multiple versions of your files, which makes it possible to revert to previous versions of the project, and also synchronizes with a web repository. Even if you've accidentally deleted your project, overwrote it, somebody stole your laptop or your hard drive accidentally collapsed, your data will remain safe. Dropbox also allows you to share projects with several developers.

You can also use a tool called GIT for the same purposes, but its configuration would require the "while" article. Dropbox is perfect for our current intents and purposes.

Creating a project

Run VS and select File -> New -> Project in the application menu.

Creating a new project in Visual Studio - Visual Basic (VB.NET) Basic Constructs

In the New Project window, select the Visual Basic template -> Windows and in the next menu select Console Application. Let's name this project FirstApplication. Then, switch the target framework to the .NET Framework 3.5 if you want to support Windows 7 users without additional framework installation (for Windows 8 and above choose .NET 4.5). Create a folder for your projects in your Dropbox folder, for example, "vbnet". Use the Browse button and select a folder C:\Users\your_name\Dropbox\vbnet\. We will stick with console applications ,command line interface, for a while because it needs minimal knowledge of the object-oriented world, and they are ideal for learning the basics of the language. The window should look like this:

Creating a new console application in Visual Basic .NET - Visual Basic (VB.NET) Basic Constructs

Confirm the form.

Visual Studio Tutorial

The window now looks like this (I have resized it to fit :) ):

Visual Studio window - Visual Basic (VB.NET) Basic Constructs

We're interested mainly in the middle window in which VS will generate some source code. It may be a surprise to some of you that we are not starting out with an empty window, but then again that is exactly what a template is (easily modifiable preset code)! We'll keep it simple for now, everything will be explained throughout the courses.

The first line is telling us we're creating a module. It's just a way how we structure code in VB.NET, we'll explain them in further lessons. The key will be the Main() method for us, we'll write our code between this line and the line with End Sub, i.e. the body. Main is a reserved word, and Visual Basic knows that this method must be executed as first after the application starts (there are more methods, but more on that later). Then the module is ended. Endings in VB.NET are actually pretty nice when compared to C-like languages and their curly brackets. Actually, we'll ignore everything except for the Main() method.

The second important element in the window is a green "Play" button in the upper bar, which compiles and runs our program. You can try it, but our program will not do anything, just turn terminate right away. You can also run the program with the F5 keyboard shortcut. Shortcuts in VS are designed very well and it will simplify your work a lot, that is if you decide to learn them.

Near the arrow icon, we have Debug selected. This means that the program will be compiled in the Debug mode, and it'll contain certain routines to debug errors. It's mainly used to test the program when we're developing it, and the program may run slower due to it. Once you're sure that your program is complete, switch it to Release mode and run the program. As a result, we'll have a program which can be distributed among people.

Directory structure of the console application

Let's take a look at what's in our application folder. Open it up, mine's in C:\Users\your_name\Dropbox\vbnet\FirstApplication\, and find a file named FirstApplication.sln. "Sln" stands for " Visual Studio solution ". It's a set of projects and it may contain multiple applications which are used for multi-tier applications, or for testing. We'll open our applications using this file. There should also be a folder names FirstApplication/ containing our project. Let's open it up, and see what's inside.

The FirstApplication.vbproj file contains a project file that we can use to open up our app. Module1.vb contains the actual source code. We are also interested in the bin/ folder, whose name suggests that it contains binary (machine) code of our application.

Inside the bin folder, you'll see that it contains Debug/ and Release/ subfolders. There are .exe files of our application inside of it (that is if we run the application at least once in the corresponding mode). In order to show your application to somebody, this .exe file in the Release/ subfolder is the file you would have to send him or her. Don't worry about any of the other files, for now.

Hello world

As tradition instructs, we'll make the first program most people make when they learn a new language - Hello World. This is a program that displays "Hello world" or some similar text. Again, let me remind you that we'll write commands in the body of the Main() method. We'll need two commands (note: I'm using the word command just for simplification): first one to display the text and the second one to wait for the user to press any key to prevent the program from terminating immediately.

To write text we use the following bit of code:

Console.WriteLine("Text")

Then, we wait for the user to press a key:

Console.ReadKey()

Console is a Class. For now, we'll see classes as sets of available commands. In VB.NET, commands are called methods. So console includes methods for console operations. We call the WriteLine() method from it that displays a text. You can see that we use a dot operator to call the method in the class. Methods can receive input parameters, which are entered in parentheses and separated by commas. In the case of the WriteLine() method, the parameter is the text to be printed. In programming, texts are called strings, like a string of characters, and written it in quotation marks. VB.NET sees the quotation marks and interprets it as text. The ReadKey() method has no parameters, but we still have to add parentheses after the method name because it is VB.NET needs it to be this way. We should write the commands on separate lines. Our Main() method will now look like this:

Sub Main()
    Console.WriteLine("Hello ICT.social!")
    Console.ReadKey()
End Sub

You can run the program by pressing F5 if your system hasn't assigned a different command for the key. You can also run code snippets from our articles by pressing the "Play" button above, you can edit the code as well, just click on it. Try that :) This makes you able to study from anywhere, even without your IDE.

Your first VB.NET app
Hello ICT.social!

Congratulations, you have just become a programmer :) That will be all for today, in the next lesson, Variables and type system in VB.NET, we'll look at the basic data types and create a simple calculator.

Today's project is attached as a file at the end of the article. You could always download the result below each lesson. I suggest that you create a project using the tutorial, and if something in your project doesn't work, then download it to find a mistake. If you download the lesson code before even trying to make it yourself, you won't learn anything :)


 

Did you have a problem with anything? Download the sample application below and compare it with your project, you will find the error easily.

Download

By downloading the following file, you agree to the license terms

Downloaded 67x (50.19 kB)
Application includes source codes in language VB.NET

 

Previous article
Introduction to Visual Basic .NET and the .NET framework
All articles in this section
Visual Basic (VB.NET) Basic Constructs
Skip article
(not recommended)
Variables and type system in VB.NET
Article has been written for you by Michal Zurek
Avatar
User rating:
17 votes
Activities