Created
July 11, 2017 18:09
-
-
Save awright18/8b4da20bf10f73346e09711ab3aaab80 to your computer and use it in GitHub Desktop.
A way to avoid having to use enums
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace NotEnums | |
{ | |
public class Options | |
{ | |
public string Value { get; private set; } | |
private const string Option1 = "Option1"; | |
private const string Option2 = "Option2"; | |
private const string Option3 = "Option3"; | |
public Options(string value ) | |
{ | |
Value = value; | |
} | |
public static Options FirstOption = new Options(Option1); | |
public static Options SecondOption = new Options(Option2); | |
public static Options ThirdOption = new Options(Option3); | |
} | |
public class OptionsConsumer | |
{ | |
public void ConsumeOptions(Options options) | |
{ | |
//this is using the default equality comparer and since | |
//the types are static they will always be equal if the same named the same. | |
//You can't use switches this way, but you should try to avoid switches anyway. | |
if (options == Options.FirstOption) | |
{ | |
//option 1 | |
Console.WriteLine(options.Value); | |
} | |
if (options == Options.SecondOption) | |
{ | |
//option 2 | |
Console.WriteLine(options.Value); | |
} | |
if (options == Options.ThirdOption) | |
{ | |
//option 3 | |
Console.WriteLine(options.Value); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment