SmartEnum
SmartEnum copied to clipboard
[Question] What is the best way to use a 'SmartEnum' as a nullable or predefined 'default' parameter in a method?
Hello!
Example:
I often like to write methods and extension methods with nullable or predefined values, such as:
I get a warning (see the green underline under the null value in the method's parameter) that this type cannot/should not be null. It compiles and run's fine, but I must put the SmartEnum as null. If I try to use a default value I will receive this error:

FindSmartProperties: This method will search a list of properties, and find the most similar property by comparing the name of smartPropertyType to the list of SmartProperties. I would like to filter by dataType so I can search for exclusively properties which as Guid's or string's for example. What the method does is not related to the question, but it might help in understanding what I am attempting to achieve.
Question:
Without using overload methods on the SmartEnum , and without creating a native enum type, is there a way where I can pass a SmartEnum as a parameter in a method as I have shown above?
If there is a recommended pattern I would love some insight!
I have created a static validation method which assigns a default enumeration. My base class looks like this:
public abstract class FlightClass : SmartEnum<FlightClass, byte>
{
public static readonly FlightClass Coach = new CoachClass();
public static readonly FlightClass PremiumEconomy = new PremiumClass();
public static readonly FlightClass Business = new BusinessClass();
public static readonly FlightClass First = new FirstClass();
public FlightClass(string name, byte value) : base(name, value) { }
public abstract decimal BonusPercent { get; }
The Validation methods use the TryFromXXX methods:
public static FlightClass Validate(string name, bool ignoreCase = false)
=> TryFromName(name, ignoreCase, out var match) ? match : Coach;
public static FlightClass Validate(byte value)
=> TryFromValue(value, out var match) ? match : Coach;
Not sure if this is what you are looking for but it worked for this sample test case.
@jaldrin very cool! Thanks for sharing the code! That is very useful for the TryFromXXX methods!
Not quite what I am looking for regarding this question in particular, but still very useful!! I will use it in my methods in my projects!
This question is about 'how do I pass a SmartEnum to a method as a parameter with a default value? Such as I can easily do this:
public enum StopLightColor
{
Red,
Yellow,
Green
}
```1
````csharp
////////////////////////////////////////////////////////////////////////////////////////////////////
/// <summary>
/// This method tells me if it is safe to drive my car when I approach a stoplight on the road.
/// If not parameters are passed to the method, the color of stoplight defaults to red.
/// </summary>
///
/// <param name="colorOfStopLight"> (Optional) Color of the stop light. </param>
/// <returns> True if we can i drive forward, false if not. </returns>
////////////////////////////////////////////////////////////////////////////////////////////////////
public bool CanIDriveForward(StopLightColor colorOfStopLight = StopLightColor.Red)
{
if (colorOfStopLight == StopLightColor.Red || colorOfStopLight == StopLightColor.Yellow)
{
return false;
}
return true;
}
Other random question - how do I make my code colored and pretty like yours in comments?
@jeffward01 I don't think the language will let you do that. See this thread on Stack Overflow:
https://stackoverflow.com/questions/23036827/why-cant-i-give-a-default-value-as-optional-parameter-except-null
Basically you can default it to null, and then put a check in your code like this:
if (colorOfStopLight is null) colorOfStopLight = StopLightColor.Red;
It's not quite as nice as having the default in the method signature, but it gets the job done.
HTH.
Other random question - how do I make my code colored and pretty like yours in comments?
When you specify the triple grave accent/backtick characters you can add the language name (ie. csharp, js, etc.).
``` csharp if (colorOfStopLight is null) colorOfStopLight = StopLightColor.Red; ```
if (colorOfStopLight is null) colorOfStopLight = StopLightColor.Red;
I just saw this in a video today that you can also use the language type of diff as follows having the original lines preceded by a - sign and the replaced lines with a + sign. I thought that this was pretty cool and can be helpful.
For example: ``` diff - Old Line + New Line ```
Becomes:
- Old Line
+ New Line