Visual Basic has traditionally been associated with visual development, as is suggested by its name . But now you can also build applications using command-line tools, and you can create console applications that do not have a Windows user interface. In this section we will first preview using Visual Studio .NET to create a Windows application, and then we look at the new feature of console applications. Visual Studio .NET The Visual Basic development environment is Visual Studio, which has also provided a visual tool for other languages, such as Visual C++ and Visual J++. With Visual Studio 6.0 there was a separate IDE for the different languages. All this changes with Visual Studio .NET. In keeping with the .NET Framework, in which different .NET languages share the Common Language Runtime (CLR) and the .NET Framework Class Library, Visual Studio .NET provides one IDE that can be used by all the .NET languages. You can refer to Appendix A for help in coming up to speed on Visual Studio .NET. If you have used Visual Studio before, you should find the new version quite intuitive and easy to use. When you start Visual Studio .NET and create a new project (File New Project), you will see a window in which you can select different languages in the left panel, and you can choose from among many different project types in the right panel. See Figure 3-1. Figure 3-1. You can create different kinds of projects in different languages using Visual Studio .NET. If you would like to try a little experiment, create a new Visual Basic project using Windows Application (the default) as the template. Navigate to the Demos directory for this chapter, C:\OI\NetVb\Chap03\Demos , for the Location, and accept the suggested name WindowsApplication1 . Click OK. You will now see this Visual Studio main window, which presents a rather elaborate user interface for the new project, as illustrated in Figure 3-2. There are various menus and toolbars . There is a palette of various controls, which you can drag onto a form. There is a property panel in which you can set properties for controls and for the form itself. You can easily add handlers for form events and control events, such as clicking a button. In short, the development paradigm is just like classic Visual Basic. Figure 3-2. Main window in Visual Studio for a new Windows Application project. Beginning in Chapter 7 we will learn all about creating Windows applications, but right now, just build the project. You can use either Build Build Solution or Build Build WindowsApplication1 from the menu. (These are the same in this case, because the solution has only one project.) You can also use corresponding toolbar buttons, which are shown visually on the menu. You can run the application from the menu Debug Start or Debug Start Without Debugging. Again, there are corresponding toolbar buttons. This "starter" application simply displays a blank form, as shown in Figure 3-3. There are the standard control buttons on the top of the form, and you can close the form in the usual way by clicking the "X" button at the top right of the form. Figure 3-3. Starter application is just a blank form. Console Applications One of the new things about VB.NET is that you can now create console applications, which provide a simple command-line user interface in a DOS window. If you would like to try another simple experiment, create another new project in Visual Studio. In the Templates panel you will have to scroll to find the Console Application project type. Again, make the location the Demos directory for this chapter and accept the suggested name of ConsoleApplication1 for your new application. For a console project, there is no form. Instead, Visual Studio shows a code window. Figure 3-4 shows the main window for the new project, where we have added just a single line of code, creating a simple "Hello, world" application. We will discuss this code in the next section. Figure 3-4. Main window in Visual Studio for a new Console Application. For now, just build and run the application. If you run the program without debugging, a console window will be created and will stay open, and you will see the following output: Hello, world Press any key to continue If you run under the debugger, a console window will open briefly and then immediately close. You will need to do something in your program, such as prompt for input, to ensure that the console window stays open. We discuss this issue later in the chapter. Console Applications Versus Windows Applications Just what is the difference between a console application and a Windows application? Under .NET, there actually are four different kinds of executables that can be built, distinguished by a compiler option and a flag in the executable IL file. The four options are -
Windows Application -
Console Application -
Class Library -
Module We will discuss class library and module in Chapter 9. Using Visual Studio, you can build a project as either a Windows Application, a Console Application, or a Class Library. As an example, let's see what happens if we build WindowsApplication1 as a Console Application. In the Visual Studio Solution Explorer pane on the right side of the main window, right-click over the project WindowsApplication1 and choose Properties from the context menu. A property page comes up. In the combo box for Output type choose Console Application, as illustrated in Figure 3-5. Figure 3-5. Changing the output type to Console Application. What do you suppose will happen when you build and run the application using the modified setting? The project builds fine, and when you run it, the blank form appears as before. But now, a console window is also displayed, which will stay open as long as the application is running. If you run it under the debugger, the console window stays open after the application window is closed and displays the message Press any key to continue You could actually write to this console window in your program code, which is a technique that is sometimes useful in debugging Windows applications. So the compiler setting is actually quite trivial. What really distinguishes a Windows application is the program code, which will involve using the Windows Forms classes, as discussed in Chapter 7. In this chapter and in the rest of Part 2 we will create exclusively console applications, allowing us to focus on the VB.NET programming language without being distracted by learning about Windows Forms. Let's begin simply, with "Hello, world." We will also learn about compiling using the command line. |