I am holding structured read only data in enum type, now I would like to extend structure and for every value in enum add additional fields. So, my original enum is:
public enum OutputFormats { Pdf, Jpg, Png, Tiff, Ps };
and I want to extend them like so:
Value=Pdf
FileName="*.PDF"
ID=1
Value=Jpg
FileName="*.jpg"
ID=2
...and so on.
An enum can't hold a multidimensional data structure, so what is generally considered the best "place" to hold such structured data? Should I create a class with value
, filename
, and id
properties and initialize the data in the class constructor?
Perhaps this pseudo-enum pattern will be useful:
public class OutputFormats
{
public readonly string Value;
public readonly string Filename;
public readonly int ID;
private OutputFormats(string value, string filename, int id)
{
this.Value = value;
this.Filename = filename;
this.ID = id;
}
public static readonly OutputFormats Pdf = new OutputFormats("Pdf", "*.PDF", 1);
public static readonly OutputFormats Jpg = new OutputFormats("Jpg", "*.JPG", 2);
}
Another variation, perhaps more concise:
public class OutputFormats
{
public string Value { get; private set; }
public string Filename { get; private set; }
public int ID { get; private set; }
private OutputFormats() { }
public static readonly OutputFormats Pdf = new OutputFormats() { Value = "Pdf", Filename = "*.PDF", ID = 1 };
public static readonly OutputFormats Jpg = new OutputFormats() { Value = "Jpg", Filename = "*.JPG", ID = 2 };
}
Yes, create a class OutputFormat with Value, Filename and ID properties. You could store the data in an XML file and parse the XML file to a List, or you could initialize the OutputFormat objects somewhere in the code.
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