0

I have something like this:

static int cantryagain=0;

private void myfunction(){

if (cantryagain==0)
{
    if(variableA=1)
        {
        //do my stuff
        //ta daaaa
        }
        else
        {
        //do something magical that will help make variableA=1 but 
        //if the magic doesnt work i only want it to try once.
        tryagain();
        }
    }
}

private void tryagain
{
    myfunction();
    cantryagain=1; //to make sure the magic only happens once, but 
            //obviously it never gets here as it does
            //myfunction again before it ever can... 
}

I know this code is super lame. I'm fairly new to c#.

How could I correctly make something like this?

user2078674
  • 165
  • 2
  • 2
  • 14

5 Answers5

2

You're looking for a loop

while(somethingNotMet){
    //do something
    somthingNotMet=false;
}
mikeswright49
  • 3,381
  • 19
  • 22
  • 1
    Just to note: `somethingNotMet` is a negative variable name, which is typically frowned upon. See http://www.makinggoodsoftware.com/2009/05/04/71-tips-for-naming-variables/ and http://stackoverflow.com/a/1228106/211627 – JDB May 10 '13 at 14:37
1

If you really want to do this without using a loop, you can use an optional parameter and call the function recursively:

private void myfunction(int recursiveCount = 0)
{
    if (recursiveCount > 1)
    {
      // give up
      return;
    }

    if (variableA == 1)
    {
      //do my stuff
      //ta daaaa
    }
    else
    {
      myFunction(++recursiveCount);
    }
}

To use it just call the function without providing the parameter:

myfunction();

greg84
  • 7,541
  • 3
  • 36
  • 45
  • 1
    recursion is an advanced topic that probably shouldn't be tackled until after the basics have been covered. – Jason May 10 '13 at 14:28
0

What you're looking for is called a do while loop!

int attempts = 0;

do
{
     hasWorked = someFunction();
     attempts ++;
}
while(!hasWorked && attempts <= 1)

While (lol) I'm here, I thought I'd show you another type of loop, called a For loop. This type of loop is used when you know how many times you want your code to run.

for(int x = 0; x < 10; x++)
{
    Console.Writeline("Hello there number : " + X);
}

This will print out:

Hello there number 0
Hello there number 1
Hello there number 2
...
Hello there number 9
christopher
  • 26,815
  • 5
  • 55
  • 89
0

yes, you want to put the function in a loop, and have the function return a boolean indicating whether or not it should be run

  private bool myFunction() {
    Random random = new Random();
    return random.Next(0, 100) % 2 == 0; // return true or false, this would be your logic to implement
  }

  public bool doSomething() {
    var tryAgain = false;
    do {
      tryAgain = myFunction(); // when myFunction returns false, the loop condition isn't met, and the loop will exit
    } while (tryAgain);
  }
Jason
  • 15,915
  • 3
  • 48
  • 72
0

If you only want to try once more

static int cantryagain=0;

private void myfunction()
{
    for (int i = 0; i < 2; i++) // will loop a max of 2 times
    {
        if(variableA=1)
        {
            //do my stuff
            //ta daaaa
            break; //Breaks out of the for loop so you don't loop a second time
        }
        else if (i == 0)  // Don't bother if this isn't the first iteration
        {
            //do something magical that will help make variableA=1 but 
            //if the magic doesnt work i only want it to try once.
        }
    }
}
Cemafor
  • 1,633
  • 12
  • 27
  • After trying the previous methods I figured it out myself but my solution was closest to this answer and works well. Thanks. – user2078674 May 10 '13 at 17:50