I am writing a simple C# console application in which I am solving the problem by using a GoTo statment to return to the label if the input for UserId is not valid.
But I am not sure if I am using the statement correctly. I would like to work around if there is a better way to solve the problem, even without using GoTo statement?
public void CreateAccount()
{
Console.WriteLine("----- Create New Account -----\n" +
"Enter following account information:\n");
getUserID:
{
Console.WriteLine("Enter a UserID (Alphanumeric; no special characters): ");
string userid = Console.ReadLine();
if (!ValidUserID(userid))
{
Console.WriteLine("Userid can only contain A-Z, a-z & 0-9. Try again");
goto getUserID;
}
if (data.IsUserInFile(userid))
{
Console.WriteLine("Userid already exists. Try again");
goto getUserID;
}
}
}
I will be using the same approach for other fields such as Pin, AccountType, Balance, AccountStatus etc. so I want to ensure that I am doing this in the right manner before I expand its use.