1

I have created a function to set multiple properties of an object at once. If a certain argument isn't passed to the function then I want to avoid setting/changing it. A simplified example of the cleanest solution I could come up with is this:

private void setObjectProperties(MyObject myObject, float param1, bool? param2 = null)
{
    myObject.param1 = param1;
    myObject.param2 = param2 != null ? (bool)param2 : myObject.param2;
}

As you can see from this method, if the function is called without param2 being passed then it will just set myObject.param2 to itself. (meaning that if nothing is passed to param2 then the myObject.param2 property is untouched)

What I have seems a bit messy to me. I'm not sure if it is the 'correct' thing to do. I have thought about method overloading but I thought that this would be even messier; In the real world situation I am passing more than two arguments to be set so I could end up with a lot of overloads.

Am I missing some obvious functionality in C# that allows me to say "Don't set this parameter if nothing is passed to it's corresponding argument"?

AGB
  • 2,230
  • 1
  • 14
  • 21
Danny Herbert
  • 2,002
  • 1
  • 18
  • 26
  • Why method overloading exists then? I would keep it standard so anyone who looks at the code knows what is going on – Khalil Khalaf May 02 '16 at 23:44
  • 1
    @FirstStep If there were 5 properties, you would need 32 overloads to cover all cases. – Rob May 02 '16 at 23:45
  • isn't it 1 or 2 or 3 or 4 or 5? @rob – Khalil Khalaf May 02 '16 at 23:45
  • @FirstStep No, because you could set: [not 1, not 2], [1, not 2], [not 1, 2], [1, 2], and so on, for 2 properties. – Rob May 02 '16 at 23:46
  • What happens if the user wants to pass in `null`? It's a valid value in many cases. That being said, no, there isn't a nice way to do it without a bunch of conditional statements. Whether they're regular if statements, or what you have. – Rob May 02 '16 at 23:54
  • @Rob It seems like the property is not nullable, so there would not be a way to set it to null. It;s not explicitly stated, but it's implied by the cast to `bool`. – D Stanley May 02 '16 at 23:58
  • @DStanley yeah, the property is not nullable. I think your answer is my best bet. Shame, I was hoping for some slick operator to save the day! – Danny Herbert May 03 '16 at 00:03
  • 1
    You want your code to do two different things depending on the value of a boolean condition. **Consider using an "if" statement when you're in that situation.** – Eric Lippert May 03 '16 at 00:07

3 Answers3

4

Am I missing some obvious functionality in C# that allows me to say "Don't set this parameter if nothing is passed to it's corresponding argument"?

Not in a slick operator, but obviously you can do

if(param2.HasValue) myObject.param2 = (bool)param2;

The only real difference is if you have any logic in your setter it will not be called in this case if the parameter is null.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
2

Try the null-coalescing operator ??:

private void setObjectProperties(MyObject myObject, float param1, bool? param2 = null, bool? param3 = null)
{
    myObject.param1 = param1;
    myObject.param2 = param2 ?? myObject.param2;
    myObject.param3 = param3 ?? myObject.param3;
}

which you can invoke like

foo.setObjectProperties(myobj, param1,
       param2: p2, 
       param3: p3);
AGB
  • 2,230
  • 1
  • 14
  • 21
0

Do it like this:

private void setObjectProperties(float? param1 = null, bool? param2 = null)
{
    if (param1.HasValue) {
      this.param1 = param1.Value;
    }
    if (param2.HasValue) {
      this.param2 = param2.Value;
    }
}

Then:

MyObject myObject = new MyObject(); 
myObject.setObjectProperties(param1: 1.2f);
myObject.setObjectProperties(param2: true);
myObject.setObjectProperties(param1: 1.2f, param2: true);
SyntaxGoonoo
  • 900
  • 8
  • 10