OK, so this is very similar - (almost the same) almost - as the other answers here.
I thought I'd provide a little different take on the other answers - just offering an alternative way of using a public
method to call...
You could define the "default" values of the properties within the Result
class, by using const
ants (or even just normal fields).
Then, set the properties' default values to those constants (or fields, whichever).
Then, when you construct a new Result()
with no Initializer, the Result
object is created with those defaults.
You can then use a Reset()
function that also uses these default values.
This can prove a little easier to maintain when it comes to refactoring, too.
Here's how you could define your Result
class's properties and method:
public class Result
{
private const string defaultMsg = "xxx";
private const bool defaultIsPositive = false;
public string Msg { get; set; } = defaultMsg;
public bool IsPositive { get; set; } = defaultIsPositive;
public void Reset()
{
Msg = defaultMsg;
IsPositive = defaultIsPositive;
}
}
Note that the public string Msg { get; set; } = defaultMsg;
is a nice short-hand way of initializing the property (I think it's only available from C#6.0).
Another way could be to do this through the constructor, like so:
public Result()
{
Msg = defaultMsg;
IsPositive = defaultIsPositive;
}
Then, to consume it's as easy as creating the object, doing whatever with it; and then calling the Reset()
method:
// create a Result object with its default values
var result = new Result();
// OR
// create a Result object with some non-default values
var result = new Result { Msg = "something", IsPositive = true };
Then, manipulate or do what you will, with your result
object.
Then, to reset the values:
... // do whatever with your result object
// reset the result to its default values
result.Reset();