-2

I want to know if I can reset a method from the method itself. That is to say, if I have a method from line 10 to 30. Do a way exist to go from the line 30 to the line 10 and reset the values? I don't need to do that, but would be useful to my program if I can.

Pd: Sorry if my english is confusing. I'm not a native speaker.

  • Normally you would use a while loop, do/while loop, or for loop, depending on your requriements. Goto (go to a label) also exists, but it isn't really the right way to go, since needing to use it means that your code probably isn't written in a good way. Variables declared in a loop will automatically be "reset" each time the loop is executed, whereas variables outside the loop will remain between loops. – ProgrammingLlama Mar 12 '20 at 05:56
  • Can you provide some code to illustrate what you want to do? – ProgrammingLlama Mar 12 '20 at 05:57
  • 2
    If you are a fan of the 80s you can use a `goto` statement , however it would be failing most code reviews and getting you a laugh from your friends – TheGeneral Mar 12 '20 at 06:04
  • 1
    Just for education: There is a statement `goto` that does that and it was used earlier. Unfortunately, it was kept in C#. But **NEVER USE `goto`!** as the code will get difficult to read. You should use loops(or maybe recursion) instead as @John mentioned. – dan1st Mar 12 '20 at 06:06
  • @dan1st enthusiastic. Most would agree the one use case is breaking out of multiple loops. I've used it a few times and the world remained intact. :) – Derek C. Mar 12 '20 at 06:19
  • @DerekC. There is `break` for this. You can mark the outer loop and break out of it... – dan1st Mar 12 '20 at 06:33
  • @dan1st Do you have an example of this? Or when you say "mark" do you mean just having some kind of flag variable? – ProgrammingLlama Mar 12 '20 at 06:46
  • `:loop1 while(whatEver){while(somethingElse){blaBlaBla();break loop1;}}` @John – dan1st Mar 12 '20 at 06:50
  • @dan1st are you sure that's valid C#? It seems to be a feature of Java, but [not C#](https://stackoverflow.com/questions/1548154/is-there-a-equivalent-of-javas-labelled-break-in-c-sharp-or-a-workaround). – ProgrammingLlama Mar 12 '20 at 06:57
  • 1
    @John sadly it is – Cleptus Mar 12 '20 at 06:59
  • I am a java developer but it shocks me if c# doesn't support that... – dan1st Mar 12 '20 at 07:33
  • 1
    @dan1st Most C# devs I know would rather prefer if it did not. – Fildor Mar 12 '20 at 07:35
  • I think goto is even worse... – dan1st Mar 12 '20 at 08:13
  • 1
    @dan1st I don't want to spam the question but I think we do not need to argue about which one is worse. They both are abominations from the age of dinosaurs. – Fildor Mar 12 '20 at 08:19

2 Answers2

4

A simple solution is that you could break your method into two separate methods. For instance, you could move lines 10 to 30 in a new method, and call it inside the main method.

public void MethodA()
{
  ...

  MethodB();  

  ...
}

public void MethodB()
{
  int a = 0, b = -1;
  string x = string.Empty;
  ...
}

As you can see, in MethodB the values are reset.

Fred
  • 3,365
  • 4
  • 36
  • 57
0
public void SomeMethod( int someParam, int execTimes )
{
    while( execTimes-- > 0 ) // will decrease execTimes and execute until execTimes < 1
    {                        // mind that the postfix -- operator will first check > 0, then decrease.
                             // so, it's for example 3 > 0, 2 > 0, 1 > 0, 0 !> 0 => 3 iterations.
       // you can init a local var with the parameter from outside.
       int localParam = someParam;
       // doYourStuff here, for example mutate localParam
       localParam += 20;
       // in next iteration, localParam will be reset to someParam value.

       // a var declared in this loop's scope will always be reset to
       // its initial value in each iteration:
       int someStartValue = 0;

       // ... some logic mutating someStartValue;
    }
}

If you want it even cleaner, you can split this up:

public void ExecNTimes( Action thingToDo, int execTimes )
{
    while( execTimes-- > 0 ) thingToDo();
}

public void Logic(int parameter)
{
   // your logic, will always start with same parameter value
}

public void Caller()
{
   ExecNTimes( () => Logic(5), 3 ); // parameter = 5, execute 3x
}

Mind that above example uses an Action delegate, to show some different approach to Fred's answer, which of course also works. Here, you could use the "Repeater"-Method for different logic, too, with less duplication in code. If you need it for one single method, only, I'd opt for Fred's, though.

For reference and where you can read up on why this works:

Fildor
  • 14,510
  • 4
  • 35
  • 67