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.



Related Articles

Comments

  • a.o.a

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

    how to take input and output in vb.net???????

    Reply
  • 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

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

Top White Papers and Webcasts

  • Is your compliance strategy relying on disconnected processes and tools that leave your organization at risk? Do your security and operations teams run into conflicts--leading to a gap between audit and remediation? Now you can make even the most complex and dynamic IT infrastructure fully secure and compliant. Read this eBook and learn how to: Improve process and close the SecOps gap Quickly detect, audit and remediate breaches Create a more agile environment to comply to regulatory mandates Better …

  • On-demand Event Event Date: May 18, 2015 While the idea of using facial and/or gesture recognition to create a modern, intuitive game seems attractive, some developers may want to leverage Unity 3D as a way to accelerate their development efforts. There are many different ways in which Intel and Unity Technologies have been working together to help speed the development of games with the Intel&reag; RealSense™ SDK (software developer kit). Check out this webcast to join a panel of experts as they …

Most Popular Programming Stories

More for Developers

RSS Feeds

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