0

I am using a ranges with a method I'm working with, and my manager has asked me to use Enum instead of integers, so that instead of having this:

public virtual int MyMethod(int value)
    {
        int result = 0;

        if (value >= 0 && value <= 3333)
        {
            result = 1;
        }
        else if (value >= 3334 && value <= 6666)
        {
            result = 2;
        }
        else if (value >= 6667 && value <= 10000)
        {
            result = 3;
        }
        return result;
    }

I work with something like this:

public virtual int MyMethod(int value)
    {
        int result = 0;

        if (value >= (int)EnumClass.Range.Low.Min && value <= (int)EnumClass.Range.Low.Max)
        {
            result = 1;
        }
        else if (value >= (int)EnumClass.Range.Medium.Min && value <= (int)EnumClass.Range.Medium.Max)
        {
            result = 2;
        }
        else if (value >= (int)EnumClass.Range.High.Min && value <= (int)EnumClass.Range.High.Max)
        {
            result = 3;
        }
        return result;
    }

The EnumClass has other Enum for other methods (i.e. Rating, Priority), so I wished for Range to remain its own variable, kinda like this:

public static enum Rating
    {
        Low = 1,
        Medium,
        High
    };

public static enum[] Range =
    {
        enum Low
        {
            Min = 0,
            Max = 3333
        },

        enum Medium
        {
            Min = 3334,
            Max = 6666
        },

        enum High
        {
            Min = 6667,
            Max = 10000
        }  
    };

I get an error while initializing that array, however, so is there any other way I could achieve this? If possible, I would like to avoid making them three Enum, but if it's unavoidable I'll work with this:

public static enum Rating
    {
        Low = 1,
        Medium,
        High
    };
public static enum RangeLow
    {
        Min = 0,
        Max = 3333
    };
public static enum RangeMedium
    {
        Min = 3334,
        Max = 6666
    };
public static enum RangeHigh
    {
        Min = 6667,
        Max = 10000
    };
3
  • 1
    Arrays contain a list of values; you appear to be asking for an array that contains a list of types. Commented Jul 16, 2012 at 15:27
  • 1
    Your manager is not a programmer is he. You don't want an enum here as this is not an enumeration. You want a set of constants with appropriate names. Commented Jul 16, 2012 at 15:32
  • @Skizz No, he isn't. I wanted to use constants as well but he is insistent on using Enum. Commented Jul 16, 2012 at 15:36

3 Answers 3

2

I think the static-constants approach is pretty straightforward, but it also consumes a lot of lines of code and can be difficult to take in at once. Here's an alternative approach that's a lot more compact -- a static list will be much simpler and clearer than a bunch of else-if statements:

// The various thresholds from your previous if statement.
var list = new List<int>() { 0, 3333, 6666, 10000 };
var min  = list.First();
var max  = list.Last();

Now you can do:

return (value >= min && value <= max) ?
  list.TakeWhile(p => p < value).Count() :
  0;

(P.S. Why is your manager dictating implementation decisions like whether to use enumerations or not, especially when it's a highly questionable usage? Unless you're doing something egregiously wrong, this feels like some pretty heavy-handed micromanagement.)

Sign up to request clarification or add additional context in comments.

2 Comments

That's what it is, actually. And thank you for your alternative approach; I'll see if I can get to use this instead.
Note that if the values you want to return aren't actually 0, 1, 2, 3, etc., just modify the second line so that it's mapped accordingly.
1

You're being asked (or worse, told) to use a wrench to pound a nail. It will work, but it is unpleasant and this is not an appropriate use for enums, as has been mentioned.

I would suggest that if this is a common application, having a range defined and checking against it, that you encapsulate the Range in a class (which would probably be just a pair of properties for minimum and maximum, as well as methods for checking values against the range and so forth), and then use that.

2 Comments

You could extend this so that the Range type is {min,max,result} and then initialise and array of them with the appropriate values. Better still (if you want to go enterprisey) read the data from an XML file (or JSON, etc). Then the MyMethod becomes an iteration of the array of Ranges. Useful is more ranges need adding at a later date or the range values change.
I'll go with that for now. Thank you.
1

It sounds like you want constants or static readonly's, not enums. Untested code below, but this should work as follows: var foo = Range.Low.Min;

public static class EnumClass 
{
    public static class Range
    {
        public static class Low
        {
            public static readonly int Min = 0;
            public static readonly int Max = 3333;
        }

        public static class Medium
        {
            public static readonly int Min = 3334;
            public static readonly int Max = 6666;
        }

        public static class High
        {
            public static readonly int Min = 6667;
            public static readonly int Max = 10000;
        }  
    }
}

That said, something like the Range class described in this CodeProject article would be more appropriate for your use case.

2 Comments

The problem with this solution is that my manager wants all Enums used in the project to be inside a single class (EnumClass), hence I can't create a class for Range only, otherwise I would've.
I edited my answer to show how that can all be nested inside of the EnumClass class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.