Understanding .NET Framework
Microsoft's popular programming language, Visual Basic, has been a favorite choice of programmers for many years. The ease with which Windows applications may be built, coupled with its wealth of database capabilities, has entrenched it in the hearts of many programmers. In its latest version, Microsoft has revamped Visual Basic, now called Visual Basic.NET (or simply VB.NET), to include full object-oriented capabilities and has provided support to work with the .NET Framework. We examine some of these issues throughout the book as we learn of the powerful services that are provided to the VB.NET programmer by the .NET Framework. In this chapter, we introduce the .NET Framework in sufficient detail so that you can immediately begin programming in VB.NET. For more in-depth information about .NET, you can refer to other books in The Integrated .NET Series from Object Innovations and Prentice Hall PTR. Of particular interest to VB.NET programmers will be Application Development Using Visual Basic and .NET (Oberg, Thorsteinson, Wyatt), which delves into many important topics that are beyond the scope of this book.
.NET: What You Need to Know
A beautiful thing about .NET is that, from a programmer's perspective, you scarcely need to know anything about it to start writing programs for the .NET environment. You write a program in a high-level language such as VB.NET, a compiler creates an executable (.EXE) file, and you run that EXE file. We show you exactly how to do that in just a few pages. Naturally, as the scope of what you want to do expands, you will need to know more. But to get started, you need to know very little.
Even very simple programs, if they perform any input or output, will generally require the use of the services found in library code. A large library, called the .NET Framework Class Library, comes with .NET, and you can use all of the services of this library in your programs.
What is really happening in a .NET program is somewhat elaborate. The EXE file that is created does not contain executable code, but rather Intermediate Language code, or IL (sometimes called Microsoft Intermediate Language or MSIL). In the Windows environment, this IL code is packaged up in a standard portable executable (PE) file format, so you will see the familiar EXE extension (or, if you are building a component, the DLL extension). When you run the EXE, a special runtime environment (the Common Language Runtime or CLR) is launched, and the IL instructions are executed by the CLR. Unlike some runtimes, where the IL would be interpreted each time it is executed, the CLR comes with a just-in-time (JIT) compiler that translates the IL to native machine code the first time it is encountered. On subsequent calls, the code segment runs as native code.
Thus, in a nutshell, the process of programming in the .NET environment goes like this:
Write your program in a high-level .NET language such as VB.NET.
Compile your program into IL.
Run your IL program, which launches the CLR to execute your IL, using its JIT to translate your program into native code as it executes.
Installing the .NET SDK
All you need to compile and run the programs in this book is the .NET Framework SDK. This SDK is available on CD, or it can be downloaded for free from the Microsoft .NET Web site at http://msdn.microsoft.com/net/. Follow the installation directions for the SDK, and make sure that your computer meets the hardware requirements. (Generally, for the SDK, you need a fast Pentium processor and at least 128M of RAM.) Part of the installation is a Windows Component Update, which will update your system, if necessary, to recent versions of programs such as Internet Explorer. The SDK will install tools such as compilers, documentation, sample programs, and the CLR.
The starting place for the SDK documentation is the .NET Framework SDK Overview (see Figure 11).
Figure 11 Homepage of .NET Framework SDK.
Installing the Book Software
The example programs found in this book are available on the Web at http://www.objectinnovations.com/dotnet.htm. Download the file Install_IntroVb.exe and run this self-extracting file. If you accept the suggested installation directory, the software will be installed in the directory OI\IntroVb on your C: drive. There are subdirectories for each chapter of the book. The directory for Chapter 1 is Chap01. Sample programs are in named subdirectories of the chapter directory, and we will refer to these sample programs simply by name, such as Hello.
Your First VB.NET Program
Although we won't actually start to examine the structure of VB.NET programs until Chapter 2, you don't have to wait to compile and run your first VB.NET program. Start at the command prompt, and navigate to the Hello directory for this chapter. (If you accepted the default installation, the directory is C:\OI\IntroVb\Chap01\Hello.) The source file is Hello.vb. To compile this program, enter the following command:
The file Hello.exe will be created, which you can now run.
>hello Hello World!
Setting Environment Variable
In order to run command line tools such as the VB.NET compiler using the name vbc rather than the complete path, certain environment variables must be set. The environment variables can be set using the batch file vsvars32.bat, which can be found in the Common\Tools directory of the Framework SDK.
If you have Visual Studio.NET installed, you can ensure that the environment variables are set up by starting your command prompt session from Start | Programs | Microsoft Visual Studio.NET 7.0 | Microsoft Visual Studio Tools | Microsoft Visual Studio.NET Command Prompt.
Although the .NET Framework SDK is all you need to compile and run VB.NET programs, the process will be much easier and more pleasant if you use the Visual Studio.NET integrated development environment (IDE). The IDE provides an easy-to-use editor, access to the compiler and debugger, and access to online help. We will discuss Visual Studio.NET in Chapter 3.
If you are eager to start learning the VB.NET programming language right away, by all means proceed directly to Chapter 2. The nice thing about a high-level programming language is that, for the most part, you do not need to be concerned with the platform on which the program executes (unless you are making use of services provided by the platform). You can work with the abstractions provided by the language and with functions provided by libraries.
However, you will better appreciate the VB.NET programming language and its potential for creating sophisticated applications if you have a general understanding of .NET. The rest of this chapter is concerned with helping you to achieve such an understanding. We address three broad topics:
What Is Microsoft .NET?
Common Language Runtime