0

This is valid:

public enum Size
{
    Eight,
    EightAndOneHalf
};

But how can numbers be used in a C# enum like the following? Is it even possible? Or is the a preferred way to represent this structure?

public enum Size
{
    8,
    8.5
};
6
  • 8.5 is not an integer. Would a little abstraction help? For Size, you could have Large and Small Commented Nov 25, 2018 at 21:50
  • You're right! Although, the 8 does not work either. And no, integers would be required for this use case, instead of "Small, Medium, Large...". Commented Nov 25, 2018 at 21:52
  • @crayden what does the number 8 alone signify? that's not how enums work. Commented Nov 25, 2018 at 21:53
  • @crayden "The enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list." as per the C# documentation. Commented Nov 25, 2018 at 21:54
  • I'm trying to define a finite set of shoe sizes. Not sure if enum is the most appropriate type to use, or another type instead. Commented Nov 25, 2018 at 21:56

3 Answers 3

4

you can use structs:

public struct Size
{
        public const double Eight = 8;
        public const double EightPointFive = 8.5;
        public const double Nine = 9;
        // Add Remaining units / values
}

And then can be used like:

double size = Size.Eight;

Or can do like this:

enum Size {
    Eight, EightPointFive, Nine
}

and then use a function like this:

public float GetSize(Size size)
{
    switch (size)
    {
        case Size.Eight :
            return 8;
        case Size.EightPointFive :
            return 8.5;
        // rest of the sizes.
        default: return -1;
    }

    //unhandled
    return -1;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Is it possible to bind this to a select list in the View?
@crayden There is a helper for that, if you want to go the enum route: learn.microsoft.com/en-us/dotnet/api/… - requires you to be using version 5.1 or newer of the MVC framework. Example: stackoverflow.com/questions/388483/…
1

If what you're after is named constants, you can create something like this:

public static class Size
{
    public const double Eight = 8.0;
    public const double EightAndOneHalf = 8.5;
}

Then you would use it like:

var mySize = Size.EightAndOneHalf;

You can't enumerate your "fake" enum, though, so you'd have to add something like this, if you want to do that:

public static class Size
{
    public const double Eight = 8.0;
    public const double EightAndOneHalf = 8.5;

    public static IEnumerable<double> All = new double[] { Eight, EightAndOneHalf };
}

Then you can do something like:

foreach(var size in Size.All)
{
    // code here
}

This basic idea is used in the framework itself. For example: https://github.com/aspnet/Identity/blob/master/src/Identity/IdentityConstants.cs

Comments

0

At their core Enums are little more then compile time constants, wich are grouped and thus get some type safety. It has the benefits of both a custom type, being only numbers for processing and using constants in code.

The default type of Enums is one of the integer ones and the default values start at 0, counting up. While the type and values can be overwritten, they can not be freely changed. It must be a integer one.

As enums effectively map to integers and are even implicitly converted (or rather left unconverted), you can do something like that with a enum/array combination.

Maybe a Dictionary<string, double> would be more fitting for your case?

Also there is a workaround: Just store 85 and 80 as the Enum values. Divide by 10 during output. Such tricks are often used to get around floating point inprecision. But it might work here too.

Personally I feel as if this falls into the area of "Internationalisation". "8.5" or "8,5" is simply how you display the value for that ShoeSize. And for Enums I always adivse to have a Array of values to output.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.