-2

for my program i'm writing i want to ask for a name and if the input has a number or other unwanted input to loop back and do it again.

This is my current code

private void gameForm_Load(object sender, EventArgs e)
{
    string value = "Type here";
    if (globalVariables.InputBox("Name", "Please enter name", ref value) == DialogResult.OK)
    {
        name = value;
        if (name.All(char.IsLetter))
        {
            lblName.Text = value;
        }
        else
        {
        }
    } 
}

How can i re-run this code if invalid input is present?

Christos
  • 53,228
  • 8
  • 76
  • 108
Ranga6669
  • 1
  • 3
  • Use a `do-while` loop. – Rahul Jul 19 '14 at 22:51
  • *Form_Load* is not a good place to implement that logic. Use a loop in another form/code, and create this form if the input satisfies your criteria. – EZI Jul 19 '14 at 22:55
  • 1
    Write another method called EnterName(). Move your code into that method. Call it from your Load event handler. Call it from where ever else you need it. Easy peasy. – Hans Passant Jul 19 '14 at 22:57

3 Answers3

0

Create a new method public void NameCheck()"Then put the code you want to run in there"

Then in you're page load you put NameCheck(); to call the method.

So if the name contains numbers you call the NameCheck(); again in the else.

    public void NameCheck()
    {
        string value = "Type here";
        if (globalVariables.InputBox("Name", "Please enter name", ref value) == DialogResult.OK)
        {
            name = value;
            if (name.All(char.IsLetter))
            {
                lblName.Text = value;
            }
            else
            {
                NameCheck();
            }
        }
    }


    private void gameForm_Load(object sender, EventArgs e)
    {
        NameCheck();
    }
Creator
  • 1,502
  • 4
  • 17
  • 30
-1

you can use a while loop

while(true)
{
   if (globalVariables.InputBox("Name", "Please enter name", ref value) == DialogResult.OK)
    {
        name = value;
        if (name.All(char.IsLetter))
        {
            lblName.Text = value;
            break;
        }
    }
}
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • I didn't downvoted and I don't see a reason for downvote as well but I think a `do-while` loop would be better here. – Rahul Jul 19 '14 at 22:53
  • i didn't do it and i actually changed it to this as it works better @Rahul – Ranga6669 Jul 20 '14 at 02:55
  • @Ranga6669, Comment was not for you; rather in general. Moreover, I knew very well that you didn't down voted cause down voting requires 250 reputation which you don't have. – Rahul Jul 20 '14 at 05:22
  • @Rahul troll algud. How would this be done to use numbers instead of a string? – Ranga6669 Jul 20 '14 at 06:17
-1
    private void gameForm_Load(object sender, EventArgs e)
    {
        reAsk:
        string value = "Type here";
        if (globalVariables.InputBox("Name", "Please enter name", ref value) == DialogResult.OK)
        {
            name = value;
            if (name.All(char.IsLetter))
            {
                lblName.Text = value;
            }
            else
            {                      
                goto reAsk;
            }
        }
    }
khan
  • 4,479
  • 1
  • 15
  • 9
  • This seems like it'll be the easiest way to do it but when i add ":reAsk" it gives me a red line and wont work is this a variable i dont have or something ?? @Khan – Ranga6669 Jul 20 '14 at 01:02