Building Console Applications

Console applications are often called command-line applications, DOS applications, and so on. They are non-GUI applications that run in the command window, and are often used for administrative tasks such as changing settings. Many Microsoft applications are still console applications. For example, in Windows 2000, you can type IPCONFIG at a command prompt to get information regarding the current IP settings on a machine. You can add command-line switches to obtain more detail or to release and renew your IP address.

Building console applications would seem to be very simple to do, and in fact, this section will be brief, because building them is simple. However, VB6 did not natively create console applications. You could do it, but it wasn't easy without the help of third-party utilities.

When you create a console application, you start without a designer, similar to what you see when you start a new class library. You also start with a Sub Main, which is what runs when the application starts. Control is basically top to bottom through the Sub Main routine, meaning that console applications tend to be written as top-down, traditional applications. These console applications are not event driven, as are other VB .NET applications.

Building a Console Application

You can build your first console application by starting a new project. Choose the Console Application project type, and name it LearningConsole. The project opens, and the only files you have are the AssemblyInfo.vb and a file named Module1.vb. Module1 is where you will be creating your console application.

In your code window, you create a module, not a class, called Module1. In all the other projects you've created so far, you have had a class created for you. This is not the case with a console application. Inside the module is a Sub Main.

To work with the console, you will accept user input and output data to the console. Outputting information to the console is relatively simple. Make your Sub Main code look like this:

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

Build this application, and then open a console window. You'll have to navigate to the default directory, which can be rather deep. The default will be c:documents and settings<username>my documentsvisual studio projectslearningconsolein. When you're there, simply type LearningConsole (your application was compiled into LearningConsole.exe) and you should see the output Hello, World! appear in the window. You can see this in Figure 10.4.

Figure 10.4. Your first console application outputting information to the console.


As you can see, this is a very simple example. It is also possible for a console application to read data from the console so that it can operate on it. For example, you might want to create a simple calculator to add two numbers. Modify your code in Sub Main to look like this:

Sub Main()
   Dim firstNum As Double
   Dim secondNum As Double
   Console.Write("Enter the first number: ")
   firstNum = Console.ReadLine
   Console.Write("Enter the second number: ")
   secondNum = Console.ReadLine
   Console.WriteLine("The sum is: " & firstNum + secondNum)
End Sub

In this application, you are creating two variables of type Double. Then, you use the Write method of the Console object. Write outputs data but does not go to the next line, so the cursor remains on the line of the output. Then, the user can type in the first number. After the user presses the Enter key, the Console.ReadLine reads everything the user typed in, and assigns it to the firstNum variable. Note that there is no error checking here, so it would be easy to cause an error by entering text data here.

After getting the second value, WriteLine is called and used to output a string and the sum of the numbers. Then, the End Sub is encountered, and the application ends. You can see an example of this application in Figure 10.5.

Figure 10.5. A console application accepting user input in the console window.


Handling Command-Line Arguments

Most console applications accept command-line arguments or command-line parameters. To handle command-line arguments, you use the same basic mechanism as you did in earlier versions of Visual Basic: You use the Command() command. Command reads all the arguments on the command line. You can then parse the arguments using the Split method of the String class in .NET.

For example, make your code look like the following:

Sub Main()
   Dim args As String = Command()
   Dim argsArray() As String = args.Split
   Dim loopCounter As Integer
   For loopCounter = 0 To UBound(argsArray)
      Console.WriteLine(argsArray(loopCounter))
   Next
End Sub

In this code, you first read the arguments added after the application name using the Command function. You then split the values into a one-dimensional array using the string's Split method.

You then enter a loop that goes from zero (remember, in VB .NET, all arrays are zero-based) up to the UBound of the array. For each element, you write the value out to the console line. Figure 10.6 shows you an example of adding a couple of command-line arguments to the application, and having the console application parse those arguments and output them to the console.

Figure 10.6. A console application accepting command-line arguments and parsing them into an array, which is then echoed back to the console window one element at a time.


Is this all you can do with console applications? Of course not. However, most of the functionality you will need can be handled by Read, ReadLine, Write, and WriteLine. It is possible to change the input and output streams, but you have to be running in unmanaged mode to do this, something Microsoft warns you against.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.216.71