Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an enum as an optional parameter

Tags:

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.

like image 628
El Kabong Avatar asked Feb 07 '14 00:02

El Kabong


People also ask

How do you set a parameter as optional?

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.

Why is enum not good?

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.

Can enum be parameterized?

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.

Can an enum be a parameter in Java?

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.

How do you pass enum as parameter in spring boot?

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.


2 Answers

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); 
like image 129
JleruOHeP Avatar answered Sep 18 '22 06:09

JleruOHeP


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 an ArgumentOutOfRangeException exception when you set the StopBits property to None.

like image 27
Jonathon Reinhart Avatar answered Sep 21 '22 06:09

Jonathon Reinhart