This article was contributed by Karl Moore.
Environment: Visual Basic
Very few developers ever attempted to create DOS-style console applications by using previous versions of Visual Basic because it was just too darn difficult. Ever craving for a challenge, I went the whole hog—and even wrote an entire magazine feature showing exactly how it could be done. But, admittedly, it was bloomin' difficult.
To get started, simply create a new project of the Console Application type. You'll be shown the simplest of Code windows—one module containing one method, Main. This Main method is the procedure that will run by default when your application fires.
Figure 1: Say hello to the home of your console code!
What can you do from here? Well, console applications have really just a few core commands, all of which are available through the System.Console namespace, intrinsically available to your program. Let's cover the important ones now.
First, you need to know how to spurt something out at the user. The following code will print the specified line out to the console:
Console.WriteLine("Welcome to the Automatic Data Processor")
One way of just appending text to the current line is by using the Console.Write method. Here's an example:
Console.Write("Processing ...") ' start processing Console.Write("...") ' more processing Write("... finished")
You'll also want to know how to receive input from the user. This can be done through the Console.ReadLine function, as so:
Dim strResponse As String Console.WriteLine("Enter your age: ") strResponse = Console.ReadLine Console.WriteLine("You are " & strResponse & " years old")
How about adding blank lines? It's as simple as writing out an empty string:
And what about incorporating a pause, for a "press return to continue" moment? Just do a Console.ReadLine without bothering to record the results:
Console.WriteLine("Press return to continue...") Console.ReadLine()
After that, you can continue coding away as normal. Add functions, call Web services, and read and write files. Your console application is no different from any other in the .NET range, except that it lacks any real user interface. And, if you're writing one of those applications that performs a bundle of data processing at 3:00 A.M. or executes a routine administrator task, that's absolutely perfect. Those sorts of applications suit the formal, no-frills console application look wonderfully.
Well, that's all you really need to get going with your own console application. Simply press F5 to run and test, or compile (select Build > Build Solution from the menu), open up a command window, and run the exectuable in your Bin folder. Enjoy!
Top Tip: Console applications include built-in support for reading from text files (rather than pausing for user input) and outputting to text files (rather than sending text to the screen). To take advantage of this, use the SetIn and/or SetOut methods of the Console object, passing in new TextReader and/or TextWriter objects, respectively. For more information, look up "Console class, about Console class" in the Help index.
Another Top Tip: Want your console application to run at set times? Click Programs > Accessories > System Tools > Scheduled Tasks, then double-click 'Add Scheduled Task' and follow the wizard.
Figure 2: My sample console application in action
About the Author
Karl Moore (MCSD, MVP) is an experience author living in Yorkshire, England. He is author of numerous technology books, including the new Ultimate VB .NET and ASP.NET Code Book (ISBN 1-59059-106-2, $49.99), plus regularly features at industry conferences and on BBC radio. Moore also runs his own creative consultancy, White Cliff Computing Ltd. Visit his official Web site at www.karlmoore.com.