methods - C# Optional parameters / multiple required -
is possible requirement 2 optional parameters together? here's example:
public void paramchoise(string a, bool b = false, string c = "y") { // stuff here }
where b optional, want function require c if b true. logic that:
public void paramchoise(string a, (bool b = false, string c = "y"))
couldnt find googling. 1 possible way me be:
/// <summary> /// if want behaviour xxx enabled, please enter "c" usage. /// </summary> /// <param name="a"></param> /// <param name="c"></param> public void paramchoise(string a, string c = "y") { // stuff here }
for sure give function comment this, writing comment unlogical given parameter feels bad me.
this case maybe bad example im sure i'll run again in future.
thanks :).
edit clear things:
possible combination parameters:
- a only
- a & (b & c)
- not & b
- not & c (because b gives point if c needed)
i think need old way - method overloading.
// b = true public void paramchoise(string a, string c) // b = false public void paramchoise(string a)
they should call private version of original
private void paramchoiseinternal(string a, bool b = false, string c = "")
you should ensure you're giving 2 public methods name accurately conveys meaning - programmer (probably you, know) call right method current state.
after update
same above, names reflect requirement, , should still able call original private method. overloads ensure consistency
public void aonly(string a) public void aandbandc(string a, bool b, string c)
struggling decipher these 2:
not & b without c !!!
not , c (because b gives point if c needed)
Comments
Post a Comment