Is it possible to use Enum with Pair Values ​​as a dictionary

In C #, I'm a little puzzled to understand Enum.

In my particular case, I will need to store a constant value in the format of the name value, for example>

300 seconds = 5 minutes

I am currently using this class.

  • Could Enum be used instead, so that the Enum class would look pretty?
  • Can I store Enum a Pair values?

Could you provide me a sample code?

using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MyWebSite.Models { public class Reminders { private sortedDictionary<int, string> remindersValue = new SortedDictionary<int, string>(); // We are settign the default values using the Costructor public Reminders() { remindersValue.Add(0, "None"); remindersValue.Add(300, "5 minutes before"); remindersValue.Add(900, "15 minutes before"); } public SortedDictionary<int, string> GetValues() { return remindersValue; } } } 
+6
source share
5 answers

You can use the key Tuple<int, int> as a dictionary (at least with .NET> = 4).

But since you really want to save TimeSpan , use this as a key.

 private static Dictionary<TimeSpan, string> TimeSpanText = new Dictionary<TimeSpan, string>(); static Reminders() { TimeSpanText.Add(TimeSpan.Zero, "None"); TimeSpanText.Add(TimeSpan.FromMinutes( 5 ), "5 minutes before"); TimeSpanText.Add(TimeSpan.FromMinutes( 15 ), "15 minutes before"); TimeSpanText.Add(TimeSpan.FromMinutes( 30 ), "30 minutes before"); TimeSpanText.Add(TimeSpan.FromHours( 1 ), "1 hour before"); // .... } public static string DisplayName(TimeSpan ts) { string text; if (TimeSpanText.TryGetValue(ts, out text)) return text; else throw new ArgumentException("Invalid Timespan", "ts"); } 

You can get the translation in this way:

 var quarter = TimeSpan.FromMinutes(15); string text = TimeSpanText[ quarter ]; 
+8
source

You can decorate your listing with description attributes and access them later through reflection. For instance,

 enum ReminderTimes { [Description("None")] None = 0, [Description("5 minutes before")] FiveMinutesBefore = 300, [Description("15 minutes before")] FifteenMinutesBefore = 900 } 

You can get a description:

 public static string GetDescription(this Enum value) { FieldInfo field = value.GetType().GetField(value.ToString()); DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute; return attribute == null ? value.ToString() : attribute.Description; } 

See also: http://www.codeproject.com/Articles/13821/Adding-Descriptions-to-your-Enumerations

+5
source

An enumeration is actually a named integer type. For instance.

 public enum Foo : int { SomeValue = 100, } 

which means that you are creating an Foo enum with type 'int' and some value. I personally always make this explicit to show what is happening, but C # implicitly makes it "int" (32-bit int).

You can use any name for enumeration names and you can check if it is a valid enum using Enum.IsDefined (for example, to check if 300 is a valid rename name).

Update

Well, actually this is not 100% correct, to be honest. This update is just to show what is really happening under the hood. An enumeration is a value type with fields that act as names. For instance. the above listing is actually:

 public struct Foo { private int _value; public static Foo SomeValue { get { return new Foo() { _value = 100 }; } } } 

Note that 'int' is an int type (explicit in my case). Since this is a value type, it has the same structure as a real integer in memory, which is probably what the compiler uses to create it.

+3
source

If you ask if you can store an integer value with respect to an enumeration, then yes, you can, for example,

  public enum DurationSeconds { None = 0, FiveMinutesBefore = 300, FifteenMinutesBefore = 900, ThirtyMinutesBefore = 1800, OneHourBefore = 3600, TwoHoursBefore = 7200, OneDayBefore = 86400, TwoDaysBefore = 172800 } 
+2
source

Unlike what I usually do, I will add another answer, which is the IMO answer to the problem.

Usually you want the compiler to do as many checks as you can before using runtime checks. This means in this case using Enum to get the values:

 // provides a strong type when using values in memory to make sure you don't enter incorrect values public enum TimeSpanEnum : int { Minutes30 = 30, Minutes60 = 60, } public class Reminders { static Reminders() { names.Add(TimeSpanEnum.Minutes30, "30 minutes"); names.Add(TimeSpanEnum.Minutes60, "60 minutes"); } public Reminders(TimeSpanEnum ts) { if (!Enum.IsDefined(typeof(TimeSpanEnum), ts)) { throw new Exception("Incorrect value given for time difference"); } } private TimeSpanEnum value; private static Dictionary<TimeSpanEnum, string> names = new Dictionary<TimeSpanEnum, string>(); public TimeSpan Difference { get { return TimeSpan.FromSeconds((int)value); } } public string Name { get { return names[value]; } } } 

When creating such a program, the language helps you in several ways:

  • You cannot use time frames that are not defined
  • Initializes the dictionary only once, or rather: when the type is built
  • In Enum.IsDefined, make sure that you are not using the wrong int value (for example, the new Reminders ((TimeSpanEnum) 5) will fail.
0
source

All Articles