I have several methods in an application I'm working on loaded with optional parameters, some of which are enums. Currently, in order to do that I'm writing methods with a similar type of signature:
public void SomeMethod(string myFirstParam = "", string mySecondParam = "", MyEnum myThirdParam = (MyEnum )(-1)){ if (myThirdParam != (MyEnum ) (-1)) { //do something with it } }
So my first question is, is there some pitfall to this approach I haven't realized, but in time will become painfully aware of, and secondly, is there a more proper - or at least elegant solution to it?
I should say that we control the input to this method, it's used internally, so I'm not worried about someone casting in a value of -1 to gum up the works.
We can implement optional parameters by assigning a default value for the parameters. This is the easiest and simple way to make the methods parameter optional. In this way, we just need to define the optional parameter with its default values when we create our methods.
They Lack Accountability. To be clear, it's a mistake to assume consensual non monogamy is void of commitment. It's not simply informing your casual hookup of your other casual hookups. For a lot of people, it's being communicative to their partner of what they're doing with their friend with benefits or boyfriend.
We have to create parameterized constructor for this enum class. Why? Because as we know that enum class's object can't be create explicitly so for initializing we use parameterized constructor. And the constructor cannot be the public or protected it must have private or default modifiers.
Enum as a method argument or argument in java In the examples below i will demonstrate you how to use Enum type as a method parameter, how to return a value from any Enum type, the method will be universal method which will work with any Enum class you give it to it.
Use Enums as Request Parameters mode=ALPHA, the request parameter is a String object. Spring can try to convert this String object to an Enum object by using its StringToEnumConverterFactory class. The back-end conversion uses the Enum. valueOf method.
I would suggest using nullable enum in this situation, like this:
public void SomeMethod(string myFirstParam = "", string mySecondParam = "", MyEnum? myThirdParam = null) { if (myThirdParam.HasValue) { var enumValue = myThirdParam.Value; //do something with it } }
and you can use it like this:
SomeMethod(myThirdParam: MyEnum.Something);
Make sure your enum
has a default value (equal to zero), that means "none" or "invalid". This would be an appropriate value for your optional parameter's default value.
This is recommended by Microsoft Code Analysis as well, CA1008: Enums should have zero value.
For example:
enum SpeakerType { None = 0, Woofer, Midrange Tweeter }
This way the default
keyword provides a value that is sane, but doesn't unintentionally refer to something you don't want it to.
As an example, the BCL uses this same concept. The number of stop bits to use for a SerialPort
is defined by the StopBits
enum:
public enum StopBits { None, One, Two, OnePointFive, }
However the None
value is invalid. In fact,
The
SerialPort
class throws anArgumentOutOfRangeException
exception when you set theStopBits
property toNone
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With