Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# initialized object values

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

like image 959
Norgul Avatar asked Oct 19 '16 14:10

Norgul


1 Answers

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 
like image 176
ΦXocę 웃 Пepeúpa ツ Avatar answered Oct 02 '22 12:10

ΦXocę 웃 Пepeúpa ツ