1

Is there a way to "tell" a method with optional parameters to use the default values, beyond omitting the parameter? I imagine the syntax would look something like this:

DoSomething(myParam: default)                         //calling the method

public void DoSomething(int myParam = 5) { ... }      //method definition

The reason for doing this is to avoid ambiguity between an overload of this method that takes no parameters. In other words, I don't want this method to be called:

public void DoSomething() { ... }

Thanks in advance.

7
  • 1
    The easiest way is to give them different names.. Commented Jul 18, 2012 at 16:05
  • 1
    Why do you have both the method with no parameters and the method with all parameters optional? If I were to try to use your code, I would be confused as to the different between the two (of course, you should document it, and then I could understand, but I'd recommend you avoid that situation if possible). Commented Jul 18, 2012 at 16:05
  • @TimS. - I have several public methods like GetObjectById() GetObjectByName, ... etc, including a catch-all GetObject(); all of these call a private GetObject(..) that uses optional parameters. This is because the caller of these functions doesn't support optional parameters. Commented Jul 18, 2012 at 16:10
  • 1
    Since GetObject(..) is private, I'd suggest renaming it. Commented Jul 18, 2012 at 16:16
  • 1
    @friendlycello The standard practice is to have one overload with all of the parameters that runs the actual code, and then lots of other methods that just call the one big overload and specify one or more of the parameters. I've added an answer with an example of this. Commented Jul 18, 2012 at 16:18

2 Answers 2

2

If you don't want the user to call the DoSomething method that takes no parameters, just make it private like so:

private void DoSomething();
Sign up to request clarification or add additional context in comments.

7 Comments

There are other choices, sure. 1. give the methods different names 2. use reflection 3. add an additional parameter to the 1st method (that's unused) so that the optional "real" parameter case doesn't match the signature of other overloads. 4. remove the other method. 5. add a parameter (possibly unused) that's not an int to the other method.
Well all of those solutions change the methods themselves, not just their access specifier, I answered under the assumption that he wanted to keep the same names with the same parameters.
Well, the reflection method doesn't. Beyond that, I answered under the assumption that he needs to keep the same access specifiers. Something needs to change (other than using reflection). It's simply a question of what you want to change. Changing access specifiers is one option, changing the signatures (in one of many various ways) is another route.
6. using explicit interface implementations could be another option. Make at least one of the two methods an explicit definition of an interface.
@Servy You're correct, saying "I can't think of any other way to accomplish this" was wrong, but I think that changing the access specifier is the simplest thing to do - changing the signature of the methods can be problematic.
|
2

It seems like in this particular case you just shouldn't be using optional parameters at all. They are designed to make your life easy. When they stop doing that it's probably time to just stop using them.

You can accomplish the same general goal of optional parameters with various overloads using this general approach:

public void Foo(int param1, int param2, int param3){ /* actual Foo code goes here */ }

public void Foo(int param1, int param2) { Foo(param1, param2, 5); }

public void Foo(int param1) { Foo(param1, 0, 5); }

public void Foo() { Foo(42, 0, 5); }

This is what optional parameters were designed to avoid, but sometimes going back to this boilerplate code can be simpler, even if it bloats the code a tad.

Comments

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.