What is the best “place” to store read-only structured data?

I keep only read-only structured data in the enum type, now I would like to expand the structure and add additional fields for each value in enum. So my initial listing:

public enum OutputFormats { Pdf, Jpg, Png, Tiff, Ps }; 

and I want to expand them like this:

 Value=Pdf FileName="*.PDF" ID=1 Value=Jpg FileName="*.jpg" ID=2 

... etc.

An enumeration cannot contain a multidimensional data structure, so what is generally considered the best "place" to store such structured data? Should I create a class with value , filename and id properties and initialize the data in the class constructor?

+4
source share
5 answers

Perhaps this pseudo-enumeration 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 option, 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 }; } 
+3
source

Yes, create an OutputFormat class with the Value, Filename, and ID properties. You can store data in an XML file and parse the XML file in a List, or you can initialize OutputFormat objects somewhere in the code.

+2
source

Create a class or structure with readonly properties and fields, such as:

  struct OutputFormat { public int Id { get; private set; } public OutputFormats Format { get; private set; } public string Filename { get; private set; } public OutputFormat(int id, OutputFormats format, string filename) { Id = id; Format = format; Filename = filename; } } 
+2
source
 // using a string key makes it easier to extend with new format. public interface IOutputRepository { //return null if the format was not found Output Get(string name); } // fetch a format using a static class with const strings. var output = repository.Get(OutputFormats.Pdf); 
+1
source

I would look at using a structure for this, I think. They are ideal for data that will not be changed after creation.

http://msdn.microsoft.com/en-us/library/ah19swz4(v=vs.71).aspx

Andrew

+1
source

All Articles