0

I've seen ways to list all variables in a certain class, but is there a way to set their values?
I want a method I can call, and it can reset all variables' values to false / 0 , I know I can manually set those to false / 0, but any change to their values would mess up everything, and I'm just seeing if there's anything more dynamic / easy way that this could be done ? Currently I'm doing the following:

// These are just some of the vars that are here.
Error = false;
double GUM = 0, MUM = 0;
decimal Mass = 0, GAcc = 0, Fg = 0, FµsRes = 0, FµkRes = 0, Fµs = FµsNS.Value, Fµk = FµkNS.Value;
  • What's wrong with your current approach? You could use Reflection (`GetType().GetFields()`) but this will be rather slow. – Klaus Gütter Aug 06 '20 at 11:33
  • Because I have a lot more variables, and it would be a pain to reset them, and I would like to explore other options. – DeluxeTroyer Aug 06 '20 at 11:35
  • What ways have you seen to "list all the variables" and have you investigated whether the same techniques would allow you to set their values? (I'd be surprised to see something that allowed the first without the second...) – Jon Skeet Aug 06 '20 at 11:36
  • Isnt that if you new a class'again , all the values will be automatically set to false/0 ? so classWithVars = new ClassWithVars() and then it's reset. Or thats not what you need? – mike Aug 06 '20 at 11:38
  • I've seen people using reflection, but they don't delve into setting values more so as listing them. – DeluxeTroyer Aug 06 '20 at 11:39
  • It would, but I've got some values in a Button_Click event. – DeluxeTroyer Aug 06 '20 at 11:42

1 Answers1

0

As others have mentioned, you could use Reflection to set all the class fields to their default values:

using System;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var myClass = new MyClass();
            myClass.Error = true;
            myClass.GUM = 1;
            myClass.MUM = 2;
            myClass.Mass = 3.5m;
            myClass.GAcc = 4.5m;
            myClass.Fg = 5.5m;

            ResetFields(myClass);

            // All fields are reseted
        }

        public static void ResetFields(object source)
        {
            foreach (var fieldInfo in source.GetType().GetFields() )
            {
                fieldInfo.SetValue(source, GetDefault(fieldInfo.FieldType) );
            }
        }

        public static object GetDefault(Type type)
        {
            if (type.IsValueType)
            {
                return Activator.CreateInstance(type);
            }

            return null;
        }
    }

    public class MyClass
    {
        public bool Error = false;

        public double GUM = 0, MUM = 0;

        public decimal Mass = 0, GAcc = 0, Fg = 0;

        //etc etc
    }
}

A better approach however, is to create a class to only hold the values and re-create it when needed:

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var myClass = new MyClass();
            myClass.Variables = new MyVariables();
            myClass.Variables.Error = true;
            myClass.Variables.GUM = 1;
            myClass.Variables.MUM = 2;
            myClass.Variables.Mass = 3.5m;
            myClass.Variables.GAcc = 4.5m;
            myClass.Variables.Fg = 5.5m;

            myClass.Variables = new MyVariables();

            // All fields are reseted
        }
    }

    public class MyVariables
    {
        public bool Error = false;

        public double GUM = 0, MUM = 0;

        public decimal Mass = 0, GAcc = 0, Fg = 0;
    }

    public class MyClass
    {
        public MyVariables Variables;

        // etc etc

        public int Xyz
        {
            get { return Variables.GUM + Variables.MUM; } // Use calculated properties
        }
    }
}

The GetDefault method is shown in this answer.

mtanksl
  • 592
  • 6
  • 9