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.

Console.Read
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))

Console.Read()

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.

More by Author

Must Read