Question

I think using an IF statement is wrong here, not sure if there is something better for this purpose. Program works as I want it to for option 1) News but fails to work as I would like it to for option 2) Historical Prices.

Module Module1
Dim ticker
Sub Main()
    System.Console.WriteLine("Welcome")
    Module1.snd()
End Sub
Sub snd()
    System.Console.WriteLine("Pick a command:")
    System.Console.WriteLine("1) News")
    System.Console.WriteLine("2) Historical Prices/Volume")
    If System.Console.ReadLine = "1" Then Module1.News() : If System.Console.ReadLine = "2" Then Module1.hp()

End Sub
Sub News()
    System.Console.WriteLine("News, pick a command:")
    System.Console.WriteLine("1) OTCM")
    If System.Console.ReadLine = "1" Then
        System.Console.WriteLine("Give me ticker:")
        ticker = System.Console.ReadLine
        Process.Start("http://www.otcmarkets.com/stock/" + ticker + "/news")
        Module1.snd()
    End If
End Sub
Sub hp()
    System.Console.WriteLine("Historical Prices/Volume, pick a command:")
    System.Console.WriteLine("1) Google Finance")
    If System.Console.ReadLine = "1" Then
        System.Console.WriteLine("Give me ticker:")
        ticker = System.Console.ReadLine
        Process.Start("http://www.google.com/finance/historical?q=%3A" + ticker + "&ei=LWsXU9inBcj66gHZLA")
        Module1.snd()
    End If
End Sub

End Module

Was it helpful?

Solution

If you call Console.Readline then the console will wait for the user to input a line each time. Try a select case statement:

Sub snd()
  System.Console.WriteLine("Pick a command:")
  System.Console.WriteLine("1) News")
  System.Console.WriteLine("2) Historical Prices/Volume")    
  Select Case Console.Readline
     Case "1" : Module1.News()
     Case "2" : Module1.hp()
     Case Else : Module1.snd()
  End Select
End Sub

This will read a line and afterwards switch to the case that was typed in. Case else handles everything that is not "1" or "2" (calls the same sub again to wait for a new input).

For Steven: You can also read the line into a string variable and evaluate this using If/Then/Else like this:

Sub snd()
  System.Console.WriteLine("Pick a command:")
  System.Console.WriteLine("1) News")
  System.Console.WriteLine("2) Historical Prices/Volume")    
  Dim UserInput as String = Console.ReadLine
  If UserInput = "1" Then
    News()
  ElseIf UserInput = "2" Then
    Hp()
  Else
    snd()
  End If
End Sub

The trick is the same, you only read a line once and then evaluate and handle the input afterwards.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top