Introduction?
VB.NET is a major component of Microsoft Visual Studio .NET suite. The .NET version of Visual Basic is a new improved version with more features and additions. After these new additions, VB.NET qualifies to become a more smarter approach for the software developers.
Whidbey, the next generation of Visual Basic, is designed to be the easiest and most productive tool for creating .NET applications, including Windows applications, Web Services, and Web applications.
While providing the traditional ease-of-use of Visual Basic development, Whidbey also allows optional use of new language features. Inheritance, method overloading, structured exception handling, and free threading all make Visual Basic a powerful object-oriented programming language. Whidbey fully integrates with the .NET Framework and the Common Language Runtime, which together provide language interoperability, simplified deployment, enhanced security, and improved versioning support.
Visual Basic 1.0 revolutionized Windows development by lowering the barrier to entry and making a broad audience of developers more productive than ever. Building on this rich history, Whidbey offers task-oriented programmers a human readable syntax, an intuitive user interface, and tools and upgrade wizards that speed the development of Microsoft .NET-connected applications. Whidbey takes advantage of the ease of development espoused by its exceedingly popular predecessors, while adding new capabilities that enable all manner of programmers, from the beginner to the experienced corporate developer, to build applications for Windows, the Web, and mobile devices.
WHIDBEY is the new developing version of VB 7.0. Microsoft .NET is a new programming and operating framework introduced by Microsoft. All .NET supported languages access a common .NET library to develop applications and share common tools to execute applications. Programming with Visual Basic using .NET is called WHIDBEY.
First, I�ll show you how to write simplest console based �Hello World� application in WHIDBEY.
A Visual Basic program has standard building blocks.
Visual Basic code is stored in project modules. Projects are composed of files, which are compiled into applications. When you start a project and open the code editor, you see some code already in place and in the correct order. Any code that you write should follow this sequence:
1. Option statements
2. Imports statements
3. The Main procedure
4. Class, Module, and Namespace, if applicable, statements
In addition, a program may contain conditional compilation statements. These can be placed anywhere in the module. Many programmers prefer to put them at the end.
If you enter statements in a different order, compilation errors may result.
Option statements establish ground rules for subsequent code, helping prevent syntax and logic errors. The Option Explicit statement ensures that all variables are declared and spelled correctly, which cuts back on time spent debugging later. The Option Strict statement helps prevent logic errors and data loss that can occur when you work between variables of different types. The Option Compare statement specifies the way strings are compared to each other, either by their Binary or Text arrangement.
Imports statements allow you to name classes and other types defined within the imported namespace without having to qualify them.
The Main procedure is the "starting point" for your application � the first procedure that is accessed when you run your code. Main is where you would put the code that needs to be accessed first. In Main, you can determine which form is loaded first when the program is started, find out if a copy of your application is already running on the system, establish a set of variables for your application, or open a database that the application requires. There are four varieties of Main:
� Sub Main()
� Sub Main(ByVal CmdArgs() As String)
� Function Main() As Integer
� Function Main(ByVal CmdArgs() As String) As Integer
The most common variety of this procedure is Sub Main().
Classes and modules make up most of the code in your source file. They contain most of the code you write � mainly Sub, Function, Method and Event statements � along with variable declarations and other code needed to make your application run.
Conditional compilation statements can appear anywhere within the module. They are set to execute if certain conditions are met during run time. You can also use them for debugging your application, since conditional code runs in debugging mode only.
Let�s look at some actual WHIDBEY programs. Let�s start by compiling and running the short sample program shown here. �Sample WHIDBEY Program� has been a symbol of writing your first Windows applications. Our first program is �sample WHIDBEY program�. This program writes an output on the console saying �This is a sample WHIDBEY program�. As you will see, this involves a little more work than you might imagine. The program looks like Listing 1.1.
Listing 1.1: Hello WHIDBEY Program Sample.
� This is a simple WHIDBEY program.
� Call this file �Example1.vb�
Imports System
Module Module1
Sub Main()
Console.WriteLine("This is a simple WHIDBEY Program") ' Display message on computer screen.
End Sub
End Module
You can type the above code in any text editor such as NotePad or VS.NET IDE and save the file as Example1.vb.
The important points of this program are the following:
Imports statement
Comments
The Main Procedure
Input and Output
Compilation and Execution
For most computer languages, the name of the file that holds the source code to a program is arbitrary. However, this is not the case of WHIDBEY. The first thing that you must learn about WHIDBEY is that the name you give to a source file is very important. For this example, the name of the source file should be Example1.vb.
Once the file is saved, you can compile this from command line or using any IDE including VS.NET IDE. WHIDBEY compiler, vbc.exe ships with Microsoft .NET Framework SDK. You can access it either through an IDE or from command line. IDE generally provides you an option to compile your program from a menu or toolbar. To compile the Example1 program, execute the compiler from command line, vbc, specifying the name of the source file on the command line, as shown here:
c:\>vbc Example1.vb /out: Example.exe /t:exe
or simply type the command from the c:\ prompt
c:\>vbc Example1.vb
You can optionally include the /main command-line compiler option in the vbc command to specify the namespace and module supplying Main.
After compiling your code, VB compiler creates an exe HelloWorld.exe under the current directory. Now you execute exe from Windows explorer or command line. Did you see " This is a simple WHIDBEY Program" on your console? Yes?? Congratulations. You are now officially a WHIDBEY programmer.
When the program is run, the following output is displayed:
This is a simple WHIDBEY program
Although Example1.vb is quite short, it includes several key features which are common to all WHIDBEY programs. Let�s closely examine each part of the program.
The program begins with the following lines:
� This is a simple WHIDBEY program.
� Call this file �Example1.vb�
This is a comment. Like most other programming languages, WHIDBEY lets you enter a remark into a program�s source file. The compiler ignores the contents of a comment. Instead, a comment describes or explains the operation of the program to anyone who is reading its source code. In this case, the comment describes the program and reminds you that the source file should be called Example1.vb. Of Course, in real applications, comments generally explain how some part of the program works or what a specific feature does.
Note: If your comment requires more than one line, use the comment symbol on each line. You can also add comments to your code by preceding the text with the REM keyword. However, the ' symbol and the Comment/Uncomment buttons are easier to use and require less space and memory.
The following chart provides general guidelines for what types of comments should be listed in a particular section of code. These are suggestions; Visual Basic does not enforce "rules" for adding comments. Write what works best, both for you and for anyone else who reads your code:
|
Comment description |
Purpose |
Describes what the procedure does (not how it does it) |
Assumptions |
Lists each external variable, control, open file, or other element accessed by the procedure |
Effects |
Lists each affected external variable, control, or file, and the effect it has (only if it is not obvious) |
Inputs |
Specifies the purpose of the argument |
Returns |
Explains the values returned by functions |
Remember the following points:
� Every important variable declaration should include an inline comment describing the use of the variable being declared.
Variables, controls, and procedures should be named clearly enough that inline commenting is needed only for complex implementation details.
Comments cannot follow a line-continuation sequence on the same line.
You can add or remove comment symbols for a block of code by selecting two or more lines of code and choosing the Comment and Uncomment buttons on the Edit toolbar.
The next line of code is shown here:
Imports System;
Most of the .Net types are defined in namespaces. A namespace is a scope in which managed types are defined. If you see .NET Framework Class Library, you�ll see hundreds of namespace. For example, System namespace contains types such as Console, Object and so on. If you want to access Console class, you need to import System namespace in your application by using Imports directive. For example, �Imports System� namespaces uses the Console class, which is defined in the System namespace.
You can even call a namespace explicitly without using Imports keyword. The below sample example shows you �Example1.vb� example without Imports namespace, which you can see in Example2.vb program.
The next line of code is shown here:
Sub Main()
This line begins the main() method. This is the line at which the program will begin executing. One other point:main() is simply "starting point" for your application � the first procedure that is accessed when you run your code. Main is where you would put the code that needs to be accessed first. In Main, you can determine which form is loaded first when the program is started, find out if a copy of your application is already running on the system, establish a set of variables for your application, or open a database that the application requires. There are four varieties of Main:
Sub Main()
Sub Main(ByVal CmdArgs() As String)
Function Main() As Integer
Function Main(ByVal CmdArgs() As String) As Integer
The most common variety of this procedure is Sub Main().
The next line of code is shown here. Notice that it occurs inside main().
Console.WriteLine("This is a simple WHIDBEY Program")
This line outputs the string "This is a simple WHIDBEY Program" followed by a new line on the screen. Output is actually accomplished by the built-in WriteLine() method. In this case,WriteLine() displays the string which is passed to it. As you will see, WriteLine() can be used to display other types of information, too. The line begins with Console. The Console class is used to read and write to or from the system console. The WriteLine method is used to write data on the console.
As you have some experience with Visual interface tools, console output(and input) is not used frequently in real WHIDBEY programs. Since most modern computing environments are windowed and graphical in nature, console I/O is used mostly for simple, utility programs and for demonstration programs.
Perhaps no other concept is more fundamental to a programming language than that of a variable. As you probably know, a variable is a named memory location that may be assigned a value by your program. The value of a variable may be changed during the execution of the program. The next program shows how a variable is declared and how it is assigned a value. In addition, the program also illustrates some new aspects of console output. As the comments at the top of the program state, you should call this file Example2.vb.
� Here is another short example.
� Call this file �Example2.vb�.
Listing 1.: Example2.vb Sample.
Module Module1
Sub Main()
System.Console.WriteLine("This is a simple WHIDBEY program")
End Sub
End Module
Read
Reads a single character
int i = Console.Read();
ReadLine
Reads a line
string str = Console.ReadLine();
Write
Writes a line
Console.Write("Write: 1");
WriteLine
Writes a line followed by a line terminator..
Console.WriteLine("Test Output Data with Line");
Conclusion
In this article, I explained the basic introduction of Whidbey development. Still, this application is under development by Microsoft, not finally released yet by Microsoft. You will get more in touch about this application while visiting the Microsoft site.