1

I simply want to create enum of operators like this but this gives errors.

Public enum MyOperators {"<", "<=", ">",  ">="}

From suggestion on SO , i tried something like this.

Public enum MyOperators {
  Less ='<', 
  LessOrEqual = '<=',
  Greater = '>', 
  GreaterOrEqual = '>='
}

Now it does not like the '=' sign in LessOrEqual and GreaterOrEqual.

You can see how MS approaches it at MS references here for c# operators. I can do that as well but i just wanted to see if there is any better suggestion.

EDIT

If you can make this work without a list and switch statement you would answer my question

        enum MyOperators {"<", "<=", ">",  ">="}

        string s  = Console.ReadLine();
        MyOperators op;

        if (Enum.TryParse(s, true, out op)) {
            //user entered operator 
        }
        else {
            //not operator
        }
Dawit
  • 591
  • 8
  • 24
  • 3
    single quotes are for Char double quotes for Strings just fyi – Joe Feb 02 '17 at 02:58
  • 2
    The `char` approach will somewhat work, but you can't do `'<='` as a character literal because "<" and "=" are two different characters, and would be like trying to assign a value of 5 and 15 to an integer variable at the same time. The string approach will just straight up not work, and you are SOL on that front. – Abion47 Feb 02 '17 at 03:12

3 Answers3

4

An enum value is typically of type int. Consequently, you connot assign objects of type string or char. You may misunderstand the concept of an enum and when to use it.

As Abion47 said: "... An enum type can be declared to be any native integral type from byte to ulong and everything in between. [...] This does not include char. [...] char is implicitly convertible to int, so you can assign an integer enum to a character literal."

You appear to look for a list of constants. As I don't know what you want to do with your "enum" try this, it may help:

public static class MyOperators
{
    public const string Less = "<"; 
    public const string LessOrEqual = "<=";
    public const string Greater = ">";
    public const string GreaterOrEqual = ">=";
}
Community
  • 1
  • 1
Quality Catalyst
  • 6,531
  • 8
  • 38
  • 62
  • 2
    Your first sentence isn't *entirely* accurate. An `enum` type can be declared to be any native integral type from `byte` to `ulong` and everything in between. True to your point, though, this does not include `char`. (Of course, `char` is implicitly convertible to `int`, so you _can_ assign an integer enum to a character literal.) – Abion47 Feb 02 '17 at 03:08
  • You can not mark const as static. It is static by definition. – Antonín Lejsek Feb 02 '17 at 04:11
2

Per your edit, you can easily provide that functionality with a list, so I'm not sure why you wouldn't want one. For example, you can do this:

var operators = new List<string>() { "<", "<=", ">", ">=" };

string s  = Console.ReadLine();

if (operators.Contains(s))
{
    //user entered operator 
}
else
{
    //not operator
}

You could even substitute the list for an array, if you so choose.

Abion47
  • 22,211
  • 4
  • 65
  • 88
  • This is acceptable way to do it if you are dealing with small list. But if you have lets say 100+ operators this approach is not good - from easy to read, document-able code perspective. @QualityCatalyst answer is the better approach. – Dawit Feb 05 '17 at 04:01
  • @Davvit I'd actually disagree. If you store the values as constants and need to see if a string value is equal to one of them, you would have to compare the value to each constant individually. Either that, or you would need to arrange them all into a list/array anyway. QualityCatalist's approach is better for storing the values, but this is better for comparing them, which is what OP is ultimately trying to do. – Abion47 Feb 05 '17 at 04:16
0

Just saw in this answer how this is possible using the EnumMember attribute, if you are primarily concerned with deserializing something like JSON:

public enum CardType
{
    [EnumMember(Value = "<")]
    LessThan,
    [EnumMember(Value = "<=")]
    LessThanEqualTo,
    [EnumMember(Value = ">")]
    GreaterThan,
    [EnumMember(Value = ">=")]
    GreaterThanEqualTo
}
Smurph269
  • 65
  • 1
  • 6