GoTo statements and alternatives in VB.NET

Posted by qais on Stack Overflow See other posts from Stack Overflow or by qais
Published on 2010-04-23T17:14:27Z Indexed on 2010/04/23 20:33 UTC
Read the original article Hit count: 203

Filed under:
|
|
|

I've posted a code snippet on another forum asking for help and people pointed out to me that using GoTo statements is very bad programming practice. I'm wondering: why is it bad?

What alternatives to GoTo are there to use in VB.NET that would be considered generally more of a better practice?

Consider this snippet below where the user has to input their date of birth. If the month/date/year are invalid or unrealistic(using if statements checking the integer inputs size, if there's a better way to do this, I'd appreciate if you could tell me that also :D) How would I be able to loop back to ask the user again?

retryday:
    Console.WriteLine("Please enter the day you were born : ")
    day = Console.ReadLine
    If day > 31 Or day < 1 Then
        Console.WriteLine("Please enter a valid day")
        GoTo retryday
    End If

© Stack Overflow or respective owner

Related posts about homework

Related posts about vb.net