Reputation: 4783
Is there a way to change C# object values all at once...in one line?
So if I have a class Result
with string msg
and bool isPositive
and I already call the constructor somewhere before like var result = new Result();
can I change values somehow by not entering:
result.msg = "xxxxxx";
result.bool = false;
But doing something like what you can do while instantiating:
result { msg = "", bool = true}
I know I can do it by instantiating again a new object by:
var result = new Result() { msg = "", bool = true}
but that is not my question :)
Upvotes: 8
Views: 1041
Reputation: 48297
Why don't you create a method that do that for you...
public void SetResult(string msg, bool flag)
{
myFlag = flag;
myMsg = msg;
}
and then you invoke it:
myResult.SetResult("xxx", false); // one line
Upvotes: 7
Reputation: 6623
You can make a setter:
class Result
{
public string msg;
public bool boolean;
public SetValues(string message, bool boolean)
{
msg = message;
this.boolean = boolean;
}
}
Then you can call:
SetValues("a string", true);
You can also take advantage of the optional parameters:
public SetValues(string message = "", bool boolean = false)
{
msg = message;
this.boolean = boolean;
}
So you can call the method without specify any parameter, and it will work as a reset:
SetValues();
But I will still recommend to use initialization list, as making a setter which sets multiple properties is not really a good practice since you can set each one of them in various ways, and still it violates a bit the SRP.
Upvotes: 4
Reputation: 1259
Explicitly you could implement a method within your class that does what you need, for example:
public class Result
{
public string Property1 { get; set; }
public int Property2 { get; set; }
//directly in the instatation of the object
public Result()
{
this.Property1 = "a";
this.Property2 = 1;
}
//or explicitely within a method
public void ChangeValues()
{
this.Property1 = "a";
this.Property2 = 1;
}
//or explicitely setting the values from outside as parameters
public void ChangeValues(string property1, int property2)
{
this.Property1 = property1;
this.Property2 = property2;
}
}
Ussage:
//option 1
var result = new Result(); //values would be already set here
//option 2
var result = new Result();
result.ChangeValues(); //values would be set here
//option 3
var result = new Result();
result.ChangeValues("a",1);
You could also iterate thru the properties in order to set each one with different values, but that would be a bit of overkill I think
Upvotes: 2
Reputation: 3180
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();
Upvotes: 1
Reputation: 6418
You can write a function to reset values. For example:
public class Result
{
...
public void ResetResult()
{
msg = "xxxxxx";
boole = false;
}
}
Then, to perform your reset,
var result = new Result() { msg = "", boole = true}
//...Do whatever you want to do with result.
result.ResetResult();
//...Do whatever you want to do with the *reset* result.
Upvotes: 4