I need help with this please?
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
Re: I need help with this please?
Re: I need help with this please?
I dont see how CICATRIX's reference is going to help you?
He has identified the lack of parameters in your code and therefore the scope problem that persists in your approach to the problem.
he would have been better telling you that byval simply copies the value into the routine and that the scope of the variable is in the routine and not back to the variable you think it does..
you can change your scope by using byref and that way the variable value being passed in will be chaged and change the extaernal (now in scope) variable.
This is not a good way to handle variables...
you would be better adviced to use functions... these are designed to return some value back to the calling routine and you would then act on the value returned.
The reason for using functions ifs that in complex systems you do not want to rely on side effects for your processes's integrity.
hope that helped
Re: I need help with this please?
cicatrix did the right thing. He needs to understand that DisplayMenu's menuSelection parameter needs to be passed ByRef for his code to work the way he expects.
Re: I need help with this please?
Quote:
Originally Posted by
incidentals
I dont see how CICATRIX's reference is going to help you?
That... and below you write:
Quote:
Originally Posted by
incidentals
you can change your scope by using byref and that way the variable value being passed in will be chaged and change the extaernal (now in scope) variable.
The link I posted tells exactly what should be changed in the code.
Quote:
Originally Posted by
incidentals
This is not a good way to handle variables...
Why is that? If there was some 'bad way' why would the language developers leave it there? Passing parameters by reference is perfectly 'legal' and it's generally faster than passing them by value. What's good and what's bad is determined by the overall design approach. Granted, I would have done it through a function call, but that's up to the OP. He asked for help with the code he's got and I told him exactly why his code did not work according the approach he chose.