views:

148

answers:

5

In a C# Winforms (3.5) application I have added a class that contains many properties (get/set) used to stored values from a 12 form long process.

After form 12 I would like wipe out all the property values in the storing class so that the next user won't accidentally record values when starting the process at form 1.

Is it possible to erase/destroy/dispose of all property values in a class?

My class looks like this:

private static int battery;
public int Battery
{
    get { return storeInspectionValues.battery; }
    set { storeInspectionValues.battery = value; }
}
+14  A: 

Can you create a new instance of the class instead? You will end up with exactly the same object.

Edit in response to comments:

Let's say this is your class:

public class Foo
{
    private int _battery;
    private string _someOtherValue;

    public int Battery
    {
        get { return _battery; }
        set { _battery = value; }
    }

    public string SomeOtherValue
    {
        get { return _someOtherValue; }
        set { _someOtherValue = value; }
    }
}

You say you want to "erase/destroy/dispose of all property values in a class". I assume this means you would like to reset all properties to their default values. That implies something like this:

foo.Battery = 0;
foo.SomeOtherValue = null;

The same can be accomplished by doing this:

foo = new Foo();

Now foo is an instance whose properties all have their default values. Does that solve your problem?

Bryan Watts
Simple and effective.
Marc Gravell
Most definitely; instead of using what's effectively a global variable, it would be far easier and less bug-prone to just pass the state from form to form.
Aaronaught
Aaronaught - Can you provide a link that explains more fully what you are describing? I'm learning as a I go and the answer provided by yourself and Bryan Watts isn't totally clear to me.
John M
+3  A: 

If creating a new instance is not a good choice for you, you can implement a method which assign a default value to value parameters and new instances to reference parameters.

Maurizio Reginelli
This would be like a Clear() method that old C++ data structures might have.
AaronLS
Thanks Maurizio - Are there any links that would further explain your comments?
John M
A: 

You can use a using statement in a loop to limit the scope of your object.

while (i_still_have_more_users_to_process)
{
    using (MyObject myObject = new MyObject())
    {
    // Do stuff with forms and myObject
    }
}
JeffH
-1. There's no indication that the class implements `IDisposable`, and even if it does, that's not guaranteed to set fields/properties to their default values.
Aaronaught
Please help me understand - creating a new MyObject won't result in fields/properties of that MyObject being set to their default values? Is there more to your comment than the fact that I could new the object each time at the top of the loop and "using" is unnecessary because there's no evidence of anything that needs to be disposed?
JeffH
+1  A: 

There is no built-in mechanism to "reset" the values of all properties of a class. While there are ways that you could accomplish this, both straight-forward (create a method that explicitly resets all property values) and not straight-forward (using Reflection to find all properties and set their values), I would not recommend any of those approaches for what it sounds like you're trying to accomplish.

If you have a user interface that is capturing data, submitting that data somewhere, and then discarding it, then most likely you will want to just create a new instance of your object instead of attempting to clear it out.

I notice in your example that your property has a backing variable that is static. Unless you have a particular reason for doing that, you should probably make your variables non-static, otherwise creating a new instance of your object won't really have the effect you desire (read up on the difference between static and non-static variables if that doesn't make sense to you).

Added the following code example in response to comments:

You could pass your data object between forms as a constructor argument or as a public property on each form. Your code, for instance, could look something like the following, where each form has a "Next" button that, when clicked, closes the current form and opens the next form using the same data object. The MyDataClass object is passed to each form as a constructor argument. The last form does not have a "Next" button but instead has a "Save" button that will of course save the data:

public partial class Form1
{
    private MyDataClass _Data;

    public Form1(MyDataClass data)
    {
        InitializeComponent();

        this._Data = data;

        // TODO: initialize fields with values from this._Data
    }

    protected void btnNext_Click(object sender, EventArgs e)
    {
        // TODO: store field values to this._Data

        // close this form
        this.Close();

        // show the next form and pass the data object along to the next form
        Form2 form = new Form2(this._Data);
        form.Show();
    }
}

public partial class Form2
{
    private MyDataClass _Data;

    public Form2(MyDataClass data)
    {
        InitializeComponent();

        this._Data = data;

        // TODO: initialize fields with values from this._Data
    }

    protected void btnNext_Click(object sender, EventArgs e)
    {
        // TODO: store field values to this._Data

        // close this form
        this.Close();

        // show the next form and pass the data object along to the next form
        Form2 form = new Form2(this._Data);
        form.Show();
    }
}

// ...

public partial class Form12
{
    private MyDataClass _Data;

    public Form12(MyDataClass data)
    {
        InitializeComponent();

        this._Data = data;

        // TODO: initialize fields with values from this._Data
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        // TODO: store field values to this._Data

        // TODO: save the data stored in this._Data, since this is the last form

        // close this form
        this.Close();
    }
}
Dr. Wily's Apprentice
Dr. - How do I reference a class without the 'new' keyword?
John M
John, I'm not sure if I understand what you're asking. You use the 'new' keyword to create an instance of a class. An instance of a class has its own variables that are separate from other instances of the same class, so if you create multiple instances (one instance per a user's attempt to fill out the forms in your example), then each instance's data will be separate. However, this is not the case if the variables in your class are defined as 'static', meaning that the variable is actually shared across all instances of that class. Not sure if that helps you at all.
Dr. Wily's Apprentice
Dr. - I guess what is not clear is if it is possible to initialize the storage class then run through 12 different forms - each form storing to that instance - and then record values of instance - and then destroy/dispose instance.
John M
+1  A: 

If you absolutely MUST use static, which means that creating a new object will not erase the properties (because they are static) you may consider:

  • Using a singleton pattern in order to "simulate" the static behaviour.
  • You may consider creating application domains. When a new application domain is created, static classes (with their properties and fields and everything else) are recreated and static constructors are invoked again.

Hope this helps

Daniel Dolz