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

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds