VB.NET User Input

This article explains how to take input from the user in VB.NET.

Unlike most languages, VB.NET offers a variety of ways for you to take input from the user. In this article, we’ll discuss three different functions that can be used, the Read() function, the ReadLine() Function and the ReadKey() Function.

There are other more user friendly ways of taking input in VB.NET, such as through the use of input prompts. Feel free to look those up as well.

Read()

The Read Function reads only one single character from the standard input stream. It returns the next character from the input stream, or returns -1 if there are no more characters to be read.

Module Module1
    Sub Main()
        Dim result As String

        result = Console.Read()
        Console.WriteLine(result)
        Console.WriteLine(TypeName(result))
    End Sub
End Module

We’ll be testing the above code twice with two different inputs. First by entering a character, and then an integer. (The TypeName function is used to find the data type of an object)

a
97
String

In the above example, we entered "a" into the console, and received 97 and String as Outputs. We can see, that the Read()function accepts string values, but will return a number. And it’s not just any number, 97 is the ASCII equivalent to a. You can look up a ASCII chart for more information.

4
52
String

Once again, we receive the ASCII equivalent to the number 4. While the output may be a number, it is still in string as the variable result was defined as a String. If the variable result was defined as an Integer, the number 52 would be in integer format.


ReadLine()

The most commonly used input function is ReadLine. The ReadLine Function reads a single line from the standard input stream or the command line. Basically it reads all the characters a user types until he presses enter.

The ReadLine function returns the user input in the form of a string.

Module Module1
    Sub Main()
        Dim result As String

        result = Console.ReadLine()
        Console.WriteLine(result)
        Console.WriteLine(TypeName(result))

    End Sub
End Module

The ReadLine function is much simpler. Any Line you input into the console, it will return it back to you in String format, regardless of the data type of the input.

Hello World
Hello World
String

ReadKey()

 The ReadKey function reads only one single character from the standard input stream or command line. It is typically used when you’re giving the user multiple options to select from, such as select A, B or C. Another common example is, Press Y or N to continue.

The difference between the ReadKey and Read is that ReadKey returns a character.

Module Module1
    Sub Main()
        Dim result As ConsoleKeyInfo = Console.ReadKey()

        Console.WriteLine(result.Key)
        Console.WriteLine(TypeName(result.Key))

    End Sub
End Module

This Function is the most unique out of the three Read Functions. For starters, you must declare using the above format. ConsoleKeyInfo is actually a class, and we are creating an object of it called result. Attempting to print out result normally would only return its object name, so you have to use the result.Key attribute to print out the value. The input stream will terminate as soon as a single stroke on the keyboard is detected.

4
52
ConsoleKey

Note that it returned a type of ConsoleKey rather than integer or string.


This marks the end of the VB.NET user input article. Any suggestions or contributions for CodersLegacy are more than welcome. Any questions can be asked in the comments section below.

Also see how to output values to console in VB.NET!

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments