Input and Output with VB.NET 2010

It's pretty much a given that you're going to have to deal with input and output when writing computer programs. The real question is what kinds of input and output (I/O) will you need to process. If we're talking about a console program, you'll probably have to either process the command line itself to determine what the user wants you to do, or prompt for something. In this article we'll take a look at the different methods of handling basic input and output and how you would go about it.

Way back in the early days of BASIC there were two statements for doing I/O. The INPUT statement did just that, input something. For output, there was the PRINT statement and later PRINT USING. So to prompt a user for some input and then echo it back you would do something like:

INPUT "Please type your name", $N

PRINT "Hello "; $N

VB.NET still has those two keywords, although the usage has changed somewhat. To read or write from / to the console now requires the use of the Console class from the .NET framework. There are quite a few methods available in the Console class as you can see from Visual Studio. Open either Visual Studio 2010 or Visual Basic 2010 Express edition and create a new console application. Figure 1 shows what you will see when you start typing the word "console" followed by a period. Instead of INPUT, you'll now use ReadLine, and for PRINT, you'll use WriteLine. These two statements will read or write an entire line of text terminated by the Enter key or return character.

What you will see when you start typing the word "console" followed by a period
Figure 1: What you will see when you start typing the word "console" followed by a period

If you want to read a single character, you can use Console.Read. This comes in handy when you want to write a message to the user and then have them press a key to continue. There's also the Console.ReadKey method to read any key. Check out the example program on Microsoft's MSDN site for a good treatment of using this method. That example shows some of the other attributes and methods of the Console class like TreatControlCAsInput. Setting this attribute to true disables the normal control-c behavior, which is to terminate the program.

Figure 2: Console.Read

There are a number of options for formatting your output. If you have a real variable and you wish to display it with a specific number of digits after the decimal point, you could use the String.Format method as follows:

Dim fNum As Double = 34.56789

Console.WriteLine(String.Format("{0:F3}", fNum))

The output would then be 34.568. VB.NET will perform the rounding operation for you when it displays the result. Another example would be formatting currency. The String.Format method supports currency values as in this example:

Dim Price As Double = 99.99

Dim Tax As Double = 0.055

Dim TotalCost As Double

TotalCost = Price + (Price * Tax)

Console.WriteLine(String.Format("{0:C}", TotalCost))


This will display $105.49. Both the rounding and the '$' are added for you. Check out the Microsoft MSDN page for a complete list of all the String.Format values.

VB.NET and the .NET framework make handling input and output extremely easy. It might take a little digging to find the right format statement to make it look like you want, but it's probably there somewhere. If it's not, you could use custom formatting to do it yourself. So go ahead and try it.

Related Articles


  • a.o.a

    Posted by mudasrah on 10/25/2014 10:24am

    how to take input and output in

  • educator

    Posted by feemin on 05/05/2013 02:59pm

    Hi, I tested your code and there is no error compiling. But when I run it, nothing is displayed on the form window. Should there has been output sth, just like our print statement in previous VB? Grateful for any reply. thanks feemin

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

Top White Papers and Webcasts

  • Thanks to the Internet of Things (IoT), physical assets are turning into participants in real-time global digital markets. The countless types of assets around us will become as easily indexed, searched and traded as any online commodity. While some industries will be tougher to transform than others – those with physical limitations, such as manufacturing, will be harder to digitize – untold economic opportunities exist for growth and advancement. Our research shows this will create a new "Economy …

  • Lenovo recommends Windows 8 Pro. "I dropped my laptop getting out of the taxi." This probably sounds familiar to most IT professionals. If your employees are traveling, you know their devices are in for a rough go. Whether it's a trip to the conference room or a convention out of town, any time equipment leaves a user's desk it is at risk of being put into harm's way. Stay connected at all times, whether at the office or on the go, with agile, durable, and flexible devices like the Lenovo® …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date