Results 1 to 3 of 3

Thread: Help

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2012
    Posts
    25

    Help

    I am trying to make a console application that when you press the number for the planet it displays its info, but i can't get it to display the info?
    Code:
    Module Module1
    
        Sub Main()
            Dim menuSelection As Integer
            DisplayMenu(menuSelection)
    
            Select Case menuSelection
                Case 1
                    Mercury()
                Case 2
                    Venus()
                Case 3
                    Earth()
                Case 4
                    Mars()
            End Select
    
    
        End Sub
    
        Private Sub Mercury()
            Console.WriteLine(" ------------------------------- ")
            Console.WriteLine("Mercury:")
            Console.WriteLine("Average Distance from the Sun: 57.9 million kilometers")
            Console.WriteLine("Mass: 3.31 x 10^23 kg")
            Console.WriteLine("Surface Temperture: -173 to 430 degrees Celsius")
    
    
        End Sub
    
        Private Sub Venus()
            Console.WriteLine(" ------------------------------- ")
            Console.WriteLine("Venus:")
            Console.WriteLine("Average Distance from the Sun: 108.2 million kilometers")
            Console.WriteLine("Mass: 4.87 x 10^24 kg")
            Console.WriteLine("Surface Temperture: 472 degreees Celsius")
        End Sub
    
        Private Sub Earth()
            Console.WriteLine("Earth:")
            Console.WriteLine("Average Distance from the Sun: 149.6 million kilometers")
            Console.WriteLine("Mass: 5.967 x 10^24 kg")
            Console.WriteLine("Surface Temperture: -50 to 50 degrees Celsius")
        End Sub
    
        Private Sub Mars()
            Console.WriteLine("Mars:")
            Console.WriteLine("Average Distance from the Sun: 227.9 million kilometers")
            Console.WriteLine("Mass: 0.6424 x 10^24 kg")
            Console.WriteLine("Surface Temperture: -140 to 20 degrees Celsius")
        End Sub
    
        Private Sub DisplayMenu(ByVal menuSelection As Integer)
            Console.WriteLine(" Select a Planet")
            Console.WriteLine("1. Mercury")
            Console.WriteLine("2. Venus")
            Console.WriteLine("3. Earth")
            Console.WriteLine("4. Mars")
            Console.WriteLine("5. End The Program")
            Console.WriteLine()
            Console.WriteLine("Enter your selection: ")
            menuSelection = CInt(Console.ReadLine())
    
            Do While menuSelection < 1 Or menuSelection > 5
                Console.WriteLine("Invalid Selection.")
                Console.Write("Please Enter 1 2, 3, or 4: ")
                menuSelection = CInt(Console.ReadLine())
            Loop
    
        End Sub
    
    
    End Module

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Help

    Change
    Code:
    Private Sub DisplayMenu(ByVal menuSelection As Integer)
    to
    Code:
    Private Sub DisplayMenu(ByRef menuSelection As Integer)
    Also, your program will crash, if you enter a non-numeric (say, letter) value.
    Change DisplayMenu to Function that returns the user input (safer way)

    Code:
    Private Function GetUserInput() As Integer
        Dim retVal As Integer = 0
        Do
            If Not Integer.TryParse(Console.ReadLine(), retVal) Then
                Continue Do
            End If
            If retVal < 1 OrElse retVal > 5   
                Console.WriteLine("Please enter 1-5")
            Else
                Return retVal
            End If        
        Loop
    End Function
    The correct way of obtaining the int value will be

    Code:
    menuSelection = GetUserInput()

  3. #3
    Fanatic Member proneal's Avatar
    Join Date
    May 2011
    Posts
    762

    Re: Help

    Yes OP. Look at
    vb Code:
    1. Integer.TryParse(Console.ReadLine(), retVal.

    If it can convert the console input into an Integer value (TryParse method) it will store the conversion into retVal, which as you should see has been declared as Integer Type for this purpose.
    Code:
    Dim retVal As Integer = 0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width