C4d
C4d

Reputation: 3282

C# Optional parameters / multiple required

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:

Upvotes: 3

Views: 6330

Answers (3)

Meirion Hughes
Meirion Hughes

Reputation: 26398

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

Upvotes: 1

Jamiec
Jamiec

Reputation: 136094

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)

Upvotes: 6

Sinatr
Sinatr

Reputation: 21989

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.

Upvotes: 1

Related Questions