Microsoft & .NETVisual BasicDiscovering Visual Basic .NET

Discovering Visual Basic .NET

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Interested in exploring Microsoft’s exciting .NET technologies, but don’t know where to begin? You’re in the right spot. Maybe you’ve tinkered around with HTML and maybe some JavaScript. Regardless, you don’t have to have a degree in computer science to jump in and begin learning. So, what are you waiting for?

In this article series, you’ll discover the fundamental information you need to begin writing real programs in the most widely used programming language in the world: Visual Basic. In this article, specifically, I’ll describe what you need to get going and how to get it. I’ll also cover some basic concepts and terms that will lay the foundation for creating your own .NET programs. Finally, before you finish this first article in the series, you will have created and run your first .NET application.

You may have heard that you need special applications to write your own programs. Microsoft sells Visual Studio .NET, for example, to professional developers. Tools like this are called IDEs (Integrated Development Environments) and they provide a comfortable place for developers to write large applications, putting at their fingertips a load of tools for writing, testing, and fixing programs. Although these applications can be helpful to programmers who do this for a living, you don’t need all that complexity (and cost!) when you are just getting started.

In fact, you don’t have to buy anything. All you need is the .NET Framework, which you can download and install for free.

Getting the .NET Framework

The .NET Framework is the foundation of all .NET applications. You’ll need two pieces:

  • The Microsoft .NET Framework Redistributable Package—The essential foundation needed on any computer to run .NET applications.
  • The Microsoft .NET Framework Software Development Kit (SDK)—This is a set of tools, examples, and help files that help you write your own .NET applications.

To get the Redistributable Package, go to:

http://www.microsoft.com/downloads/details.aspx?familyid=262D25E3-F589-4842-8157-034D1E7CF3A3&displaylang=en

To get the SDK, go to:

http://www.microsoft.com/downloads/details.aspx?FamilyId=9B3A2CA6-3647-4070-9F41-A333C6B9181D&displaylang=en

The only catch is that you need a high-speed connection—the SDK is about 108MB! If you want to download it in pieces, there’s a link along the right side of that page that will take you to another page where you can do that. There’s also a link that allows you to order the framework on a CD, if you prefer.

Run the Redistributable Package install first. You’ll have to accept Microsoft’s End User License Agreement, but there are no other decisions to make. It should take a couple of minutes.

The SDK install is only slightly more complicated. You can choose what pieces you want to install: tools, samples, and/or documentation (see Figure 1).

Figure 1. Install options for the .NET Framework SDK.

After that, you can choose a destination folder. This install takes a bit longer.

After everything’s installed, there’s one more step you should take to make things convenient for yourself. Right-click on your desktop and choose New, Shortcut from the popup menus. You’ll be prompted for the location of the item you want to create a shortcut to. Enter this (including the quotation marks):

%comspec% /k "c:program filesmicrosoft.netsdkv1.1binsdkvars.bat"

Click Next and then Finish. You’ll now have a shortcut on your desktop named sdkvars.bat. You’ll use this later when you create your first .NET application.

Windows, Web, and Console Applications

There are three major types of applications you can create with the .NET Framework: Windows applications, Web applications, and Console applications. You can use the Visual Basic .NET language when creating any type of application. The difference is in the way you interact with the user.

Windows applications are ones that run on your computer, such as Word and Excel. Web applications are simply Web pages where you can write code to make them do things and act intelligently, rather than just presenting information. Ebay, for example, is a big Web application. A currency conversion Web page might be an example of a simpler Web application.

Finally, there are console applications. In the days before Windows, the operating system of choice was DOS and you typed in your commands as text and saw the results as text. Although DOS itself is gone for good, there is still a command line in Windows that you can use to enter commands in the same way you could with DOS. Although you wouldn’t want to use it all the time, there are some tasks that can be done more easily with a command line than with Windows and mice. A console application is one that has no Windows or Web interface. It can have a simple text-based interface when you run it from a command prompt. But, console applications are most often used when you just want to do something and get it done—with little or no interaction with anyone.

In this article, I’ll demonstrate VB .NET using a console application. Why? Because console applications are by far the simplest and most straightforward applications. You won’t spend a lot of time learning about user interface issues—how to create windows, place controls, and so on. Instead, you’ll focus specifically on learning the language. Then, once you have the language down, it’ll be easy for you to go on to learn how to create Windows or Web applications.

Getting Started with Programming

If you have very little or no computer programming experience, review the next couple of sections. They define some terms and describe some basic concepts that you need to know to get started. If you’ve done some programming, feel free to skim or skip these sections.

What is a programming language?

A computer programming language is a lot like a human language. As with a human language, each computer language has a vocabulary of words. Likewise, each language has syntax rules for putting the words together in meaningful ways.

However, unlike human languages, computer languages are very precise. Human languages may have five different words that mean the same thing, whereas a computer language has only one. And, in a human language, you can put together the same sentence in several ways, reordering the words or even bending the rules of syntax now and then, and still be understood. Computers require that you follow their syntax to the letter. If you don’t, the computer won’t understand and will generate an error.

Also, human language is designed for two-way communication, whereas programming is a one-way street: You tell the computer exactly what it’s supposed to do and how it should respond to every situation. Instead of a conversation, it’s more like a recipe or a to-do list. The computer then simply follows your instructions. The computer can’t argue with you. (However, when it throws error messages your way, you may feel like it’s being argumentative!)

That’s why the words in a computer language’s vocabulary are often called commands or statements. When you put a series of commands together, the result is referred to as source code, or simply code. When you bring all the source code together, you have a computer program or application.

Compiling and running

After you create an application, you still have one problem: The computer can’t read the source code directly. The computer really only understands binary numbers—ones and zeros. A series of commands given to the computer in binary form is called machine code. After you finish writing and debugging your source code, you run a piece of software called a compiler, which translates your source code into machine code.

You only need to compile your program once. After that, you run or execute the program, causing the computer to carry out your commands. You can run the compiled code again and again. You don’t need to compile it again unless you change the source code. The machine code usually takes the form of an .EXE file, or executable. This file is distributed to those who need to use the application, people who are fondly referred to as users or, in the case of Web sites, visitors.

Note: If you already have some experience with .NET and how it works (or if you’ve worked with Java) you may know that the compiling and execution process is quite a bit more complicated than is explained here. But when you’re getting started, this is the all the detail you need to know. There’ll be plenty of time for diving into the deep end once you get Visual Basic .NET under your belt!

Your First .NET Program

There’s a long tradition in computer programming tutorials of creating a first application that does nothing more than display “Hello, World”. The idea is that, if you can get to that point, you have everything set up and working well enough to begin experimenting and learning. You are ready for your “Hello, World”.

Begin by creating a folder on your hard drive under My Documents. Name the folder HelloWorld.

Now, open Notepad. Type in the following text:

Imports System

Module HelloWorld
   Public Sub Main()
      Console.WriteLine("Hello, World!")
   End Sub
End Module

This is about the simplest program you can write in Visual Basic .NET—just the essential elements. I’ll go over each of these elements in the next article. For now, I want to get you used to the process of writing, compiling, and running a program.

Save your Notepad file. Save it in the HelloWorld folder you created and name it “HelloWorld.vb”.

Important Note: Be sure to actually put quotation marks around the file name when you type it in Notepad’s Save As dialog. This informs Notepad not to add a .txt extension to your file. Check to be sure the file is named correctly after you save it. If there is a .txt extension on your file, rename the file so that it is simply HelloWorld.vb.

Leave Notepad open, but minimize it for now. Double-click on the shortcut you created on your desktop after you installed the .NET Framework (earlier in this article). This opens a black window with a text interface (see Figure 2).

Figure 2. The .NET Framework Command Prompt window.

Now, type this:

cd ..My DocumentsHelloWorld

For those of you too young to remember DOS, cd stands for change directory. You can see from the prompt that you are now in the HelloWorld folder. Now type:

vbc HelloWorld.vb

vbc starts the Visual Basic compiler. You give it the name of your source code file, HelloWorld.vb, and it is compiled into an executable file. You should see text scroll up the window in response; it looks something like Figure 3.

Figure 3. Compiling HelloWorld.vb.

If what you see doesn’t look like Figure 3, then, most likely, you typed something in wrong in Notepad. In that case, go back to Notepad and check to be sure what’s there is exactly like what appears above. Make any changes necessary, save the file, and compile it again.

Once you have compiled it successfully, you’ll notice there’s a new file in your folder: HelloWorld.exe. You can run HelloWorld.exe from the command prompt by simply typing its name:

HelloWorld

When the program runs, it displays its message (Figure 4).

Figure 4. Your first .NET application in action.

If you want to experiment, go back to Notepad and change the text to whatever you like. Now, save your file. Don’t forget to save your file! Switch over to the command prompt, compile again, and run again. This is the process you’ll go through a lot as you write programs and test them—so get comfortable with it.

Summary

This article laid the groundwork so that you can get started programming in .NET. You installed the .NET Framework on your computer, explored some basic concepts and terms and then wrote, compiled, and ran your first .NET application! In the next article, you’ll dive right into the Visual Basic language!

# # #

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories