c# - How should I encapsulate this multi-dimensional enum? -
in application i've got information can 1 of small set of values - i'd use enum hold it, ensuring valid values through type-safety @ compile time:
public enum { a1, a2, a3, b1, b2, c1 }; these enums represent multi-dimensional data (they have letter , number in example above), i'd able value associated them, e.g.
something example = something.a1; // want able query values example: example.letter; // want "a" example.number; // "1"i want 1 i've 2 possible solutions, neither of them feel 'clean', interested in people prefer, , why, or whether has better ideas.
option 1: create struct wraps enum, , provides properties on wrapped data, e.g.
public struct somethingwrapper { public value { get; private set; } public somethingwrapper(something val) { value = val; } public string letter { { // switch on value... } } public int number { { // switch on value... } } } option 2: leave enum , create static helper class provides static functions values:
public static class somethinghelper { public static string letter(something val) { // switch on val parameter } public static int number(something val) { // switch on val parameter } } which should choose, , why? or there better solution i've not thought of?
third option: second option, extension methods:
public static class somethinghelper { public static string letter(this val) { // switch on val parameter } public static int number(this val) { // switch on val parameter } } then can do:
something x = ...; string letter = x.letter(); it's unfortunate there aren't extension properties, such life.
alternatively, create own pseudo enum: this:
public sealed class { public static a1 = new something("a", 1); public static a2 = ...; private something(string letter, int number) { letter = letter; number = number; } public string letter { get; private set; } public int number { get; private set; } }
Comments
Post a Comment