It's popular for books and tutorials to say that they will show you how to build killer software for yourself on your own computer. (Heck! I've even said it!) Creating an application like that is a stretch even for people who have a lot of experience. So we're going to emphasize the opposite. Since this course is advertised as being for Beginners, the applications we will build are designed to be as simple as possible and still get the idea across. In addition, to fulfill my promise of emphasizing "cheap", I'll build one from the command line, one using SharpDevelop, the free development environment, and one using Visual Studio .NET (In our uncomplicated application, there is no difference between Visual Studio .NET Learning Edition that I recommend and the complete (that is, expensive!) version.)
The Command Line VB.NET
"Command line" refers to DOS (Disk Operating System) that used to be the actual operating system (Windows 98 and earlier) and is still available in "emulation" even in the latest Windows versions. The term "command line" (or "Command Prompt" as Microsoft labels it these days) means that you can't just click buttons and menus to do things. Anything you do has to be entered as a one line "command". For example, in Windows, you can drag and drop a file from one folder to another to move it. In DOS, you have to enter a command to MOVE X Y where X and Y are file names.
The example here was developed using Windows XP. DOS can be found in XP by selecting Start > All Programs > Accessories > Command Prompt. In order to use the command line to develop and run a VB.NET program, you must first download (or obtain on CD) the .NET Framework SDK (Software Development Kit). You can get that by going to the Microsoft site here.
Actually using the tools available on the SDK in DOS will require some serious effort to learn how to do! As I said earlier, this method is cheap, not easy. Even Jesse Liberty (in his book Learning Visual Basic .NET) takes the shortcut of using a DOS environment setup by Visual Studio to run command line examples. When the SDK is installed, it creates an environment that should allow you to run the examples here. If you have problems, there is extensive documentation available with the SDK.
Later on, we create what I call the "Two Button Form". But since we don't have buttons for a command line program, we're going to get even more elementary and just read in some characters and then display the same characters. The goal is to break the ice and get started, not to write the most amazing software application ever seen.
After you have downloaded and installed the .NET Framework SDK, open Notepad and type the following program. Save the program as a '.vb' file in a convenient directory. I've used the root directory in my example below but you should probably create a special test directory for these program files to keep your hard drive more organized.
Module Module1
Sub Main()
System.Console.WriteLine ("About VB Example")
System.Console.WriteLine ()
' Prompt user for identifier
Dim ReplyString As String
System.Console.Write("Type A or B: ")
ReplyString = System.Console.ReadLine()
System.Console.Write("You typed: " & ReplyString)
End Sub
End Module
To run this program, enter the command VBC (Visual Basic Compiler) followed by the name of the program. In order for this to work, your computer must be configured so the operating system can 'find' VBC. Ordinarily, the SDK setup program will do this automatically, but if you have difficulty, there are batch files (.BAT) in the SDK that will also do it for you. When all else fails, as a last resort, you might try reading the SDK documentation!
If everything else is correct you can compile and run the program as shown below:
A Module is a basic building block of VB programs and is something of a holdover from VB 6. When we build the next application, you will see that we use a Class instead of a Module and one Class roughly equates to one object. In this example, The VB.NET compiler simply converts the Module to a Class anyway. In this program, we've given the name Module1 to our Module. Also note that the Module is ended at the bottom. VB.NET requires well defined blocks of code.
Unless you specify otherwise, the Sub Main() is the entry point for the program. That is, it's the first procedure that is executed when you run your program. Main is where you would put the code that needs to be run first. Later on, you will see that Visual Studio will often add a parameter to your project to change the first procedure to something else, such as a form. The Sub (short for Subroutine) is carefully ended just like the Module was.
Every other executable statement in the program (the Dim statement just creates a variable and isn't actually an executable) actually uses the services of one of the main objects in VB.NET: the System.Console object. This is a great opportunity to learn about the "holy trinity" of OOP: Encapsulation, Inheritance, and Polymorphism.
Let's look more carefully at the first statement in the program:
System.Console.WriteLine ("About VB Example")
Visual Studio provides a tool called the Object Browser that lets you see the relationships between objects. Let's look at the mscorlib Namespace. (You can see a table showing what all the icons in these diagrams mean here.)
All objects in .NET inherit from a single top-of-the-heap object called, naturally, Object. You can see Object in the diagram as an Intrinsic (part of the .NET Foundation) "Bases and Interfaces" class for the Console object.
A number of different Namespaces can be found in the VB.NET library file MSCORLIB.DLL including the System Namespace. The "plus-in-a-box" icon tells you that the two namespaces above System also have other objects in them. The name "MSCORLIB" suggests exactly what it is. It's the Microsoft Core Library for .NET.
The concept of Encapsulation means that you can consider all of the objects as simply "black boxes" that provides services. Console is one of the "black boxes" inside System. Some of the services that Console makes available are actually provided by System but they are Inherited from System by the Console object. As a programmer, however, you only need to know what services the Console object ultimately provides.
The right pane of the Object browser helps you find these services. Here's a view of just the variations of the WriteLine method that are found in the Console object. This brings up the concept of Polymorphism.
Polymorphism isn't a weird religion that worships parrots. It means that the same object can take on many different forms. ("poly" meaning "many" and "morph" meaning "form") Notice that there are eighteen WriteLine methods listed by the object browser but each one has a slightly different parameter list. This is how all of the C family of languages ... and now .NET languages ... tell which WriteLine method your program wants. The number and type (there's that 'strong typing' again) of the arguments passed to a method is called the method signature. The compiler looks at that to determine which method to use even though the name is the same.
This subject gets LOTS more interesting with topics like overriding, shadowing, delegates, interfaces, and subclasses. Enough to make your head spin. For right now, all you really have to know is that objects generally have lots of methods, and that you can select the one you want in code by using different signatures.
Visual Studio .NET helps you out with a tool called Intellisense that lets you choose exactly which one of the methods you really want to use. For example, if I entered exactly the same line of code in Visual Studio, I would get the helpful hints shown. Notice that VS.NET also tells me that there are eighteen methods to choose from and helpfully suggests the second one. It even gives me the right values to fill in (the False and True constants). I did say that you got value for money with Visual Studio .NET!
Next page >
The Sharp Way To Develop Code! > Page
1,
2,
3