Enums with String Values in C#

I have been working on getting through completing a phase of the Video Processor Application, I ran into an issue. I wanted to have a enum with string values. Those of you that are familiar with the C# language know that that is not possible.

Doing a search online lead me to a post on StackOverflow about how this could be done. Some of the comments were, in my opinion, over-engineered to accomplish such a simple task.

Using the example that was given in one of the comments, I cam up with the code below for the FfMpegColor class in the Video Processor application.

public struct FfMpegColor
{
    private string Value;
    public FfMpegColor(string value)
    {
        Value = value;
    }

    public static readonly FfMpegColor Black = new FfMpegColor("black");
    public static readonly FfMpegColor Blue = new FfMpegColor("blue");
    public static readonly FfMpegColor DarkGreen = new FfMpegColor("darkGreen");
    public static readonly FfMpegColor GhostWhite = new FfMpegColor("ghostwhite");
    public static readonly FfMpegColor Green = new FfMpegColor("green");
    public static readonly FfMpegColor Maroon = new FfMpegColor("maroon");
    public static readonly FfMpegColor Orange = new FfMpegColor("orange");
    public static readonly FfMpegColor Red = new FfMpegColor("red");
    public static readonly FfMpegColor RhtYellow = new FfMpegColor("0xffc107");
    public static readonly FfMpegColor SteelBlue = new FfMpegColor("steelblue");
    public static readonly FfMpegColor White = new FfMpegColor("white");
    public static readonly FfMpegColor SaddleBrown = new FfMpegColor("SaddleBrown");

    public override string ToString()
    {
        return Value.ToString();
    }
}

The way that that this works is that you create a struct of a particular type. Then you assign that to a static property that is part of that class. To me resembles the way that a ValueObject is done for an entity.

Posted: 2023-01-30
Author: Kenny Robinson