0

I am trying to set up an enum to hold sheet metal gauges (thicknesses). Right now I have:

using System.ComponentModel;
public enum Gauge
{
    [Description("24 Gauge")]
    G24 = 239,
    [Description("20 Gauge")]
    G20 = 359,
    [Description("18 Gauge")]
    G18 = 478,
    [Description("16 Gauge")]
    G16 = 598,
    [Description("14 Gauge")]
    G14 = 747
}

My question is: Does this seem like a good place to break the rule about not using non-integral types behind an enum?

The real world values are like .0239, .0359, .0478, etc. A floating point type would probably be very unreliable, but I was considering a Decimal type. Isn't it a 96 bit integer with the decimal place shifted behind the scenes? Would that should be a reliable value or am I just asking for trouble with this idea?

gunr2171
  • 16,104
  • 25
  • 61
  • 88
Mike
  • 1,274
  • 10
  • 24
  • 2
    Wait a second, I think I've [seen this before](http://stackoverflow.com/questions/25211707/where-does-it-say-you-cannot-start-a-enum-member-name-with-a-number). – gunr2171 Aug 08 '14 at 20:44
  • 1
    you could use a custom attribute to store and return the float/decimal value – Ňɏssa Pøngjǣrdenlarp Aug 08 '14 at 20:45
  • @gunr2171 I know, but I screwed up that question by asking two things in it and now I can't roll back the changes like I do on other Stack Exchange sites. – Mike Aug 08 '14 at 20:46
  • @Plutonix now that is a good idea. That might be what I should be doing. – Mike Aug 08 '14 at 20:46
  • How about using a structure or shared properties on a class instead of an enum? – Steve Aug 08 '14 at 20:48
  • @Mike: it is exactly the same question (apart from the different title). You either need to modify that or this. – Tim Schmelter Aug 08 '14 at 20:48
  • 1
    I was just about to post an answer but it was closed. Don't use an enum, use a class with implicit conversion from/to double. Predefined values can be stored as constants in that class. You can use it pretty much like an enum, but you can define double members. – Marcel N. Aug 08 '14 at 20:50
  • 2
    @TimSchmelter, it seemed the OP did a bit of mixup with the questions. The old question had 2 questions posted, but ended up duplicating with this new question. I have fixed the older question to be what was intended (and answered), so I believe this question should be reopened, or at least not a dup of the old. – gunr2171 Aug 08 '14 at 20:51

2 Answers2

2

If I understand correctly what you want to do, then you can use a class with implicit conversion from/to double:

public class Gauge
{
    [Description("24 Gauge")]
    public const double G24 = 0.239;
    [Description("20 Gauge")]
    public const double G20 = 0.359;
    [Description("18 Gauge")]
    public const double G18 = 0.478;
    [Description("16 Gauge")]
    public const double G16 = 0.598;
    [Description("14 Gauge")]
    public const double G14 = 0.747;

    private double Value { get; set; }

    private Gauge(Double d)
    {
        Value = d;
    }

    public static implicit operator Double(Gauge g)
    {
        return g.Value;
    }

    public static implicit operator Gauge(Double d)
    {
        return new Gauge(d);
    }
}

And you can use it like this:

Gauge g = Gauge.G14;
g = 0.43;
//and so on, you can pass it as parameter to methods, etc
Marcel N.
  • 13,726
  • 5
  • 47
  • 72
1

Using values like .0239 are not allowed for enums.

See enum (C# Reference):

Every enumeration type has an underlying type, which can be any integral type except char.

...

The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong.

Community
  • 1
  • 1
AlexD
  • 32,156
  • 3
  • 71
  • 65
  • Of course, but I have read that using reflection you can make it work with other types. – Mike Aug 08 '14 at 20:44
  • @Mike Also, have a look at this post: http://stackoverflow.com/questions/672102/why-no-non-integral-enums. – AlexD Aug 08 '14 at 20:57