Todd432
Todd432

Reputation: 79

Using console.readline during console.writeline

In vb.net I am trying to write a program using the console where the console writes the line 'number of cars:', and the console assigns the number inputed after the colon to a variable. Is there a way to get the console to read a value within a line that was written. i.e can you use console.readline() within console.writeline()?

Upvotes: 0

Views: 588

Answers (1)

Nathan Tuggy
Nathan Tuggy

Reputation: 2244

You'll need to use Console.Write and then Console.ReadLine for this.

For example:

Console.Write("Number of cars: ")
Dim cars As Integer
If Integer.TryParse(Console.ReadLine(), cars) Then
  ' Do something interesting
  Console.WriteLine("{0} cars, eh?", cars)
Else
  Console.WriteLine("Couldn't tell how many cars!")
End If

Output will be something like this:

Number of cars: 3
3 cars, eh?

Upvotes: 1

Related Questions