Erase all property values from an object bound to a Winforms Form?
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 cl开发者_开发问答ass looks like this:
private static int battery;
public int Battery
{
get { return storeInspectionValues.battery; }
set { storeInspectionValues.battery = value; }
}
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?
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.
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
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();
}
}
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
}
}
精彩评论