Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reinitialize or reset the properties of a class?

I've created a class with properties that have default values. At some point in the object's lifetime, I'd like to "reset" the object's properties back to what they were when the object was instantiated. For example, let's say this was the class:

public class Truck {
   public string Name = "Super Truck";
   public int Tires = 4;

   public Truck() { }

   public void ResetTruck() {
      // Do something here to "reset" the object
   }
}

Then at some point, after the Name and Tires properties have been changed, the ResetTruck() method could be called and the properties would be reset back to "Super Truck" and 4, respectively.

What's the best way to reset the properties back to their initial hard-coded defaults?

like image 201
Dylan Bennett Avatar asked Apr 02 '09 04:04

Dylan Bennett


People also ask

How do you reinitialize an object in Java?

Have a set of default values or states for your class stored inside of it. Then write a reset() method that will restore all of these defaults within the class.

How do you reset a class in Python?

Just create a new instance - if there are no other references to the previous instance, it will be discarded naturally. In other words: normal code does not call __init__ "manually" - just create a new instance and allow the language do that.

How do you clear a class in C#?

Delete a User-Defined Class Object in C# by Assigning null Value to It. A class object is a reference variable that points to the memory location of that class. We can delete the object by assigning the null value to it. It means that the object currently contains no reference to any memory location.


2 Answers

You can have the initialization in a method instead of inlining with the declaration. Then have the constructor and reset method call the initialization method:

public class Truck {
   public string Name;
   public int Tires;

   public Truck() {
      Init();
   }

   public void ResetTruck() {
      Init();
   }

   private void Init() {
      Name = "Super Truck";
      Tires = 4;
   }
}

Another way is not to have a reset method at all. Just create a new instance.

like image 100
maxyfc Avatar answered Sep 18 '22 13:09

maxyfc


Reflection is your friend. You could create a helper method to use Activator.CreateInstance() to set the default value of Value types and 'null' for reference types, but why bother when setting null on a PropertyInfo's SetValue will do the same.

    Type type = this.GetType();
    PropertyInfo[] properties = type.GetProperties();
    for (int i = 0; i < properties.Length; ++i)
      properties[i].SetValue(this, null); //trick that actually defaults value types too.

To extend this for your purpose, have private members:

//key - property name, value - what you want to assign
Dictionary<string, object> _propertyValues= new Dictionary<string, object>();
List<string> _ignorePropertiesToReset = new List<string>(){"foo", "bar"};

Set the values in your constructor:

 public Truck() {
    PropertyInfo[] properties = type.GetProperties();

    //exclude properties you don't want to reset, put the rest in the dictionary
    for (int i = 0; i < properties.Length; ++i){
        if (!_ignorePropertiesToReset.Contains(properties[i].Name))  
            _propertyValues.Add(properties[i].Name, properties[i].GetValue(this));
    }
}

Reset them later:

public void Reset() {
    PropertyInfo[] properties = type.GetProperties();
    for (int i = 0; i < properties.Length; ++i){
        //if dictionary has property name, use it to set the property
        properties[i].SetValue(this, _propertyValues.ContainsKey(properties[i].Name) ? _propertyValues[properties[i].Name] : null);     
    }
}
like image 44
arviman Avatar answered Sep 19 '22 13:09

arviman