user1877127
user1877127

Reputation: 19

How to return in a function on one step back?

while (true)
{
    lock (groupslocker)
    {
        if (groups.Count == 0)
            break;
        else
            gr = groups.Dequeue();
    }
    string url = rand_url();
    int h = od_group_join(gr, url);
    if (h == -1)
    {
        // How to return to this line string url = rand_url(); ???
    }
    else
      .........
}

How to return from if (h == -1) to line string url = rand_url(); ???­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­

Upvotes: 0

Views: 173

Answers (4)

Martin Lottering
Martin Lottering

Reputation: 1782

You could use a recursive method call. Move that part of the code into an operation (I named it SubOperation)

static void SubOperation()
{
    string url = rand_url();
    int h = od_group_join(gr, url);
    if (h == -1)
    {
        // How to return to this line string url = rand_url(); ???
        SubOperation();
    }
    else
        .........
}

Then modify the original code to call the new operation instead?

        while (true)
        {
            lock (groupslocker)
            {
                if (groups.Count == 0)
                    break;
                else
                    gr = groups.Dequeue();
            }
            SubOperation();
        }

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500065

It sounds like you want a loop:

int h;
string url;
do
{
   url = rand_url();
   h = od_group_join(gr, url);
} while (h == -1);

I'd have renamed your methods and variables, but it's too hard to guess at a reasonable meaning from the current names. I strongly advise you to use more meaningful names, and follow .NET naming conventions.

Upvotes: 3

LonWolf
LonWolf

Reputation: 132

while (true)
{
    lock (groupslocker)
    {
        if (groups.Count == 0)
            break;
        else
            gr = groups.Dequeue();
    }
    string url = rand_url();
    int h = od_group_join(gr, url);
    while (h == -1)
    {
        url = rand_url();
        h = od_group_join(gr, url);
    }

     .........
}

Upvotes: 0

Hedrack
Hedrack

Reputation: 774

int h;
string url;
do
{
    url = rand_url();
    h = od_group_join(gr, url);
}
while(h == -1);

Upvotes: 1

Related Questions