3

Is it possible to get a requirement for 2 optional parameters together? Here's an example:

public void ParamChoise(string A, bool B = false, string C = "Y")
    {
        // Stuff here
    }

Where B is optional, I want the function to require C if B is true. From my Logic something like that:

public void ParamChoise(string A, (bool B = false, string C = "Y"))

Couldnt find anything by googling. One possible way for me would be:

        /// <summary>
        /// If you want behaviour XXX to be enabled, please enter "C" for usage.
        /// </summary>
        /// <param name="A"></param>
        /// <param name="C"></param>
        public void ParamChoise(string A, string C = "Y")
        {
            // Stuff here
        }

For sure I could give my function a comment like this, but writing a comment for an unlogical given parameter feels bad to me.

This case is maybe a bad example but Im sure I'll run into this again in the future.

Thanks very much :).

EDIT TO CLEAR THINGS:
Possible combination for the parameters:

  • A only
  • A & (B & C)
  • NOT A & B
  • NOT A & C (Because B gives the point if C is needed)
7
  • 3
    No, you can´t do param-checking in the method-header. However you may introduce two overloads for this. Anyway you may also throw an excption if one of the arguments is missing whilst the other one is provided. Commented Jul 30, 2015 at 8:58
  • But you can always determine if C is required before you invoke ParamChoice. Commented Jul 30, 2015 at 8:58
  • This will be really bad from readability point of view. Just have explicit methods with unique names; transfer the logic to the caller. Commented Jul 30, 2015 at 8:59
  • In any case, you have to ask yourself if it is better to have two different functions for "B=true" and for "B=false"; of course, they may call the same private function to their actual work. Commented Jul 30, 2015 at 8:59
  • NOT A & B - does this mean NOT as in a boolean ! or as in "I dont want" Commented Jul 30, 2015 at 9:13

3 Answers 3

6

I think you need the old way - method overloading.

// b = true
public void ParamChoise(string A, string C)

// b = false
public void ParamChoise(string A)

They should call a private version of your original

private void ParamChoiseInternal(string A, bool B = false, string C = "")

You should ensure that you're giving the 2 public methods a name that accurately conveys their meaning - that will help the programmer (probably you, I know) call the right method for their current state.


After update

Same as above, names reflect requirement, and you should still be able to call your original private method. The overloads ensure your consistency

public void AOnly(string A)
public void AAndBAndC(string A, bool B, string C)

Struggling to decipher these 2:

NOT A & B WITHOUT C !!!
NOT A AND C (Because B gives the point if C is needed)

Sign up to request clarification or add additional context in comments.

8 Comments

And, if B describes some important property, this could be reflected in two different names for the two methods.
He wants B to be optional, until C is given. That simply isn't possible without logic.
@MeirionHughes No, thats upside down. I want C to fall out until B is given.
ok, but this answer completely omits C in the second case (b = false)... It still needs to be there, its just optional when b = true.
If the first function would be (string A, bool B, string C) wouldnt that be the solution? It would require C, if B would be given...!?
|
1

You can use named arguments

public void ParamChoise(string A = "", bool B = false, string C = "Y") {...}

// call it
ParamChoise(A: "123");
ParamChoise(C: "123");
ParamChoise(C: "123", A: "123");
ParamChoise(B: true, C: "123", A: "123");

I found this extremely useful for methods with many optional parameters.

With explicit parameter name specifying you are free to add new optional parameters in any place later without destroying existing code.

2 Comments

But this isnt fixing my thing. Your 2nd 3rd call shouldnt be possible. Check my edit on the question.
Then you have to use overriding (where you expose only valid combinations) and calling this ParamChoise (which accept anything), just make it non public.
1

From what I can gather you want C to optional WHEN b is false; AS an exercise in method over-loading, I think the closest you'll get is:

void ParamChoise(string A, string C)
void ParamChoise(string A, bool B = true, string C = "Y")

You still have to decide in the caller whether to supply the Bool... which makes it pointless as you can now do:

void ParamChoiseBIsFalse(string A, string C)
void ParamChoiseBIsTrue(string A, string C = "Y")

You've just got complex logic that cannot be translated directly into a simple overload. You either put that logic in the callee or caller

1 Comment

Yeah in case of the bool its pointless. But in general Ill keep your answers to the future where I maybe face another problem where I need those conditions being possible. So the first example without the optional C is correct. Anyway, already picked my answer as @Jamiec was faster.

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.