Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best "place" to hold read-only structured data?

Tags:

c#

class

struct

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?

like image 793
Tomas Avatar asked Jan 19 '23 07:01

Tomas


2 Answers

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 };
}
like image 100
Vilx- Avatar answered Jan 30 '23 22:01

Vilx-


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.

like image 29
Sjoerd Avatar answered Jan 31 '23 00:01

Sjoerd