Taking the Visual out of Visual Basic


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.

Thankfully, with the release of Visual Studio .NET, Microsoft has realized console applications are back in fashion—and added built-in support for DOS-feel applications.

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:

Console.WriteLine("")

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.



About the Author

Karl Moore

Karl Moore (MCSD, MVP) is an experience author living in Yorkshire, England. He is the author of numerous technology books, including the new Ultimate VB .NET and ASP.NET Code Book (ISBN 1-59059-106-2), 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.

Comments

  • Console Apps too dificult in VB6?

    Posted by rhenerlau on 03/22/2005 03:49pm

    You can create a project, delete the main form, and add a module. Make sure the module has a "Sub Main()" procedure in it, and yuo can set the "Sub Main()" as your Startup Object. That's pretty easy, and it's VB-6 (not .NET)

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

Most Popular Programming Stories

More for Developers

RSS Feeds