Question code correction(enum)

elianeasmar

Well-known member
Joined
Oct 3, 2013
Messages
76
Programming Experience
Beginner
hello experts. i need your help with a code.


Console.WriteLine("type a superhero name to know his color")


Dim uservalue As String = Console.ReadLine()
Dim myvalue As superheroes
'Dim myvalue As superheroes = uservalue




While [Enum].TryParse(Of superheroes)(uservalue, myvalue) And uservalue <> "y"


Select Case myvalue
Case superheroes.batman
Console.WriteLine("black")
Case superheroes.superman
Console.WriteLine("red")
Case Else
Console.WriteLine("wrong hero")
End Select
uservalue = Console.ReadLine()


End While
Console.ReadLine()
End Sub



what's wrong in my code?
when i type a wrong hero name. the programm will exit.what should i do if when a wrong name entered ,a message pops up and the program continues to debug?
please helppp :)
thank you in advance
 
Hi,

Good try, but you have got your code mixed up a little bit.

The first thing to realise is that you are using a While loop which may or may not be execute based on the condition that you are testing for. In this case, the While loop only executes if your TryParse method returns True, which is why when a wrong Superhero name is entered your program just Exits since the While loop is just ignored.

To make sure that the code within a Loop of this sort is always executed AT LEAST once then you need to use a Do/Loop Until loop since the condition to be tested for is at the end of the loop and not at the beginning, as is the case in a While loop.

So, knowing this, you need to change your logic a bit to say:-

1) Do something at least once and get your user input.
2) Use the TryParse method to test the user input and save the RESULT in a variable
3) If the Result of the TryParse method is True then display the Superhero information otherwise display an error message.
4) Finally, test for an invalid Superhero name and then Exit the loop if it is False.

Here is an example:-

Dim uservalue As String
Dim userEnteredaValidSuperheroName As Boolean
Dim myvalue As Superheroes
 
Do
  uservalue = Console.ReadLine()
  userEnteredaValidSuperheroName = [Enum].TryParse(Of Superheroes)(uservalue, myvalue)
  If userEnteredaValidSuperheroName Then
    Select Case myvalue
      Case Superheroes.Batman
        Console.WriteLine("black")
      Case Superheroes.Superman
        Console.WriteLine("red")
    End Select
  Else
    Console.WriteLine("Wrong Hero")
  End If
Loop Until Not userEnteredaValidSuperheroName
 
Console.ReadLine()


Hope that helps.

Cheers,

Ian
 
:(
Help me Please :p
the function must return something.
i'm a bit stuck :/

Public Function rec(uservalue As String)
'Dim uservalue As String
Dim userEnteredaValidSuperheroName As Boolean
Dim myvalue As Superheroes


Console.WriteLine("entrer ...")
uservalue = Console.ReadLine()
userEnteredaValidSuperheroName = [Enum].TryParse(Of Superheroes)(uservalue, myvalue)
If userEnteredaValidSuperheroName Then
Select Case myvalue
Case Superheroes.batman
Console.WriteLine("black")
Case Superheroes.superman
Console.WriteLine("red")
End Select
Else
rec(uservalue)


'If uservalue.ToUpper = "Y" Then Console.WriteLine("Game Over, Press any key enter to exit")
End If
Return myvalue.ToString


End Function
 
Last edited:
Hi,

The reason why I asked if you understood how to create and use a Recursive routine is because this is exactly how you do NOT do it. So, for the moment, forget about Recursion and just try and get your Logic working with the type of Loop that I proposed in post No.#2.

I do not have any more time today, but I will take some time tomorrow to explain a little bit about Recursion if no one else has done so before then.

Cheers,

Ian
 
Hi,

As you already know through your own code example, Recursion is a routine that calls itself to perform some repetitive action. However recursion should never be used as a replacement for a simple loop that also performs some repetitive action.

So, what's the difference?, I hear you ask yourself.

In the simplest of terms a traditional loop such as a Do Loop or a While Loop will start something at the same point in a routine and will continue to do those same actions until a tested condition is met. To demonstrate this have a look at this routine:-

Dim myString As String
Do
  myString = Console.ReadLine
  Console.WriteLine(myString)
Loop Until mystring = "Exit"


This loop will keep asking for a string and then display that string until the string that was entered equals "Exit". Note that it always starts from the same point and performs the same actions.

However, a Recursive Routine gives you the added ability of performing the same repetitive actions on something but starting from a DIFFERENT point in a routine. What do I mean by that? One of the best ways to demonstrate a Recursive Routine is to iterate through a Directory Structure whereby you have no idea how many levels of children there are within that Directory Structure.

If you were to use a traditional loop, such as a For Loop to do this, then the For Loop can only Iterate through the Current Level of the directory that was passed to it and if you wanted to get the children of a particular directory within the For Loop then you would need to use an Additional For Loop to do so and then keep doing the same thing until all child levels had been dealt with. So, knowing that, you would end up with something like this:-

Module Module1
 
  Sub Main()
    ShowDirectoriesWithoutRecursion("d:\temp")
    Console.ReadLine()
  End Sub
 
  Private Sub ShowDirectoriesWithoutRecursion(ByVal strDirName As String)
    For Each directoryName0 As String In IO.Directory.GetDirectories(strDirName)
      Console.WriteLine(directoryName0)
      For Each directoryName1 As String In IO.Directory.GetDirectories(directoryName0)
        Console.WriteLine(directoryName1)
        For Each directoryName2 As String In IO.Directory.GetDirectories(directoryName1)
          Console.WriteLine(directoryName2)
          For Each directoryName3 As String In IO.Directory.GetDirectories(directoryName2)
            Console.WriteLine(directoryName3)
            For Each directoryName4 As String In IO.Directory.GetDirectories(directoryName3)
              Console.WriteLine(directoryName4)
              For Each directoryName5 As String In IO.Directory.GetDirectories(directoryName4)
                Console.WriteLine(directoryName5)
                For Each directoryName6 As String In IO.Directory.GetDirectories(directoryName5)
                  Console.WriteLine(directoryName6)
                  For Each directoryName7 As String In IO.Directory.GetDirectories(directoryName6)
                    Console.WriteLine(directoryName7)
                  Next
                Next
              Next
            Next
          Next
        Next
      Next
    Next
  End Sub
End Module


Yuk!! and the big problem with this is "Where do I Stop adding Additional For Loops?". The answer is you can never know when to stop.

This is where Recursion comes into it's own since what you can do is call a Routine with an Initial Starting Point and then call the same routine again, from within itself, with a DIFFERENT starting point so long as a different starting point exists to iterate through. Have a play with this example which shows how Recursion is used to iterate through the Complete Child Structure of an initial Directory level that was passed to the routine:-

Module Module1
 
  Sub Main()
    ShowDirectoriesWithRecursion("d:\temp")
    Console.ReadLine()
  End Sub
 
  Private Sub ShowDirectoriesWithRecursion(ByVal strDirName As String)
    For Each directoryName As String In IO.Directory.GetDirectories(strDirName)
      Console.WriteLine(directoryName)
      ShowDirectoriesWithRecursion(directoryName)
    Next
  End Sub
End Module


I hope that helps you with your understand of Recursion and gets you to realise that Recursion is not for you in the code that you are working with.

Cheers,

Ian
 
Big thank you for your time :)
I was trying to do the recursive method to see where i stand in .net
it worked :D
Thanks again


Imports System.Windows.Forms
Module Module1
Sub Main()


Console.WriteLine("type a superhero name to know his color")
rec("")
End Sub
Public Function rec(uservalue As String) As Boolean
'Dim uservalue As String
If uservalue.ToUpper = "Y" Then Return False
'Dim i As String


Dim userEnteredaValidSuperheroName As Boolean
Dim myvalue As Superheroes
uservalue = Console.ReadLine()
userEnteredaValidSuperheroName = [Enum].TryParse(Of Superheroes)(uservalue, myvalue)
If userEnteredaValidSuperheroName Then
Select Case myvalue
Case Superheroes.superman
Console.WriteLine("red")
Case Superheroes.batman
Console.WriteLine("black")


End Select


Else
Console.WriteLine("Wrong")
End If
Return rec(uservalue.ToString)


End Function
End Module



cheers :p
 
Back
Top