8

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 :)

3
  • 2
    No, there is not. It is a GOOD thing that you can't do this, since it hurts readability and maintainability which should always be your first concern. Commented Oct 19, 2016 at 14:16
  • You do know that the initialization syntax just compiles down to basically the same thing with just a temporary value Commented Oct 19, 2016 at 14:17
  • VB.Net has with which is similarish, there is no equivalence in C#. Commented Oct 19, 2016 at 14:17

5 Answers 5

7

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 
Sign up to request clarification or add additional context in comments.

1 Comment

Too bad C# doesn't have the feature I want, but nice workaround
4

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.

Comments

4

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.

4 Comments

Isn't this just the same as @ΦXocę 웃 Пepeúpa ツ's answer?
public bool bool; does this even compile in your pc?
@GeoffJames I was writing when he posted it. Also this answer contains the definition of the method, instead of an empty one, but it's the same principle
@ΦXocę웃Пepeúpaツ of course it does not, I have just changed it. I was wondering too if it will compile :D
2

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

Comments

1

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 constants (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(); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.