1

I have a C# winform app which is doing a lot of calculation. there is a "run" button to trigger the process. I would like to be able to "re-trigger or re-run or re-submit" the information without having to restart the program. Problem is I have a lot of variables that need to be reset. Is there a way to undef (reset) all parameters?

private Double jtime, jendtime, jebegintime, javerage, .... on and on
John Ryann
  • 2,283
  • 11
  • 43
  • 60
  • make a method and with in that method call set values to null, string.Empty, 0, and so on.. you need to provide a better example as well... and remember this "WHERE THERE'S A WILL---THERE'S ALWAYS A WAY" – MethodMan Feb 23 '12 at 20:12

4 Answers4

5

Create an instance of an object that stores these variables. Reference this object, and when wanting to "reset", reinstantiate your object. e.g.

public class SomeClass
{
   public double jTime;
   ...
}

...

SomeClass sc = new SomeClass();
sc.jTime = 1;
sc = new SomeClass();
George Johnston
  • 31,652
  • 27
  • 127
  • 172
1

The best way would have been if you had them all in a class.
Then on reset you'd just create a new class with initialized values.

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
1

You could use Reflection; although Reflection is the less performant than the other proposed solutions, but I am not entirely sure of your solution domain and Reflection might be a good option.

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Data data = new Data();

            //Gets all fields
            FieldInfo[] fields = typeof(Data).GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly);

            foreach (var field in fields)
            {
                //Might want to put some logic here to determin a type of the field eg: (int, double) 
                //etc and based on that set a value

                //Resets the value of the field;
                field.SetValue(data, 0);
            }

            Console.ReadLine();
        }

        public class Data
        {
            private Double jtime, jendtime, jebegintime, javerage = 10;
        }
    }
}
Ricky Gummadi
  • 4,559
  • 2
  • 41
  • 67
0

Yes, simply use Extract Method refactoring technique. Basically extract reset logic in a separate method and then just call it when need

private void ResetContext()
{
   jtime = jendtime = jebegintime = javerage = 0;
}
sll
  • 61,540
  • 22
  • 104
  • 156