Created
September 16, 2011 09:51
-
-
Save Immerseit/1221706 to your computer and use it in GitHub Desktop.
Combine Enum and Class for Extended functionality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The idea is to collect this kind of parsing nearest possible to the enum itself | |
// Why? Because the enum is the reason to the Method and are easy to find and modify | |
// Also it's natural to extend with other kind of parsing. Just insert more methods. | |
// The main drawback, is that I have to state the enum more explicit (i.e. Something.TypeOf) | |
// Also the code design appears no standard and would apply if the enum was for internal use in the class. | |
// So, the question | |
// How would you do this more nice? I tried abstract, base inheritance, partial. None of them apply. | |
// This is a shortened down and renamed sample of a code that should be better coded. | |
public static class Something | |
{ | |
public enum TypeOf : short | |
{ | |
Minute = 2, Hour = 3, Day = 4, ........ | |
} | |
public static long GetTicks(Something.TypeOf someEnum) | |
{ | |
long ticks = 0; | |
switch (someEnum) | |
{ | |
case Something.TypeOf.Minute: | |
ticks = TimeSpan.TicksPerMinute; | |
break; | |
case Something.TypeOf.Hour: | |
ticks = TimeSpan.TicksPerHour; | |
break; | |
.... | |
} | |
return ticks; | |
} | |
} | |
// This class is called from anywhere in the system. | |
public static void SomeMethod(string dodo, object o, Something.TypeOf period) | |
{ | |
// With the design above | |
long ticks = Something.GetTicks(period); | |
// Traditional, | |
if (period == Period.Day) | |
ticks = TimeSpan.FromDays(1).Ticks; | |
else if (period == Period.Hour) | |
ticks = TimeSpan.FromHours(1).Ticks; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment