0

I'm trying to get a List of strings of the values of an Flags Enum

I have two enums

enum example {
 name0 = 0,
 name1 = 1,
 name2 = 2,
 name3 = 3
}
//And a flagged enum
[Flags]
enum example2 {
 none = 0,
 name1 = 1 <<example.name1,
 name2 = 1 << example.name2,
 name3 = 1 << example.name3
}

I have a flagged enum setted.

example2 = name1 | name2;

What I'm trying to do, is, from that example2 = name1 | 2; get a List of strings that has the integer values of the first enum.

So far I've tried to make a List of strings of the flagged enum: example:

example2.toString()

//result: "name1, name2"
//I'm not quite sure how to proceed with this, I've read the documentation but can't find something helpful, probably to split and trim the string to get a list of names, then iterate over that list and somehow get the numeric value using the names
/* 
   result I'm trying to achieve:
   ["1", "2"] <-- List of strings of int values corresponding to those names.
*/

Does anyone know a good way to do this?

~This is my first question, sorry if the explanation was bad.

  • Can you explain better what you are trying to achieve? Why are you using 1 enum to reference another enum? – nalnpir Oct 24 '20 at 00:58
  • @nalnpir I have a flag enum setted exampleEnum = name1 | name2 , that is being sended to the server, and I have to do a conversion from that to a List of Strings with the numeric values of those ["1", "2"], from the not flagged enum. If I try to get numeric values from the flagged enum I would get powers of two ["2", "4"], I think that's the reason for 2 enums, I didn't write the code at first. – José Luis Flores García Oct 24 '20 at 01:06
  • I feel like we either need to know why you're trying to do this so we can show you a better way, or you need to narrow the question to something more specific, like "how to get the integer value of an enum", "how to get the string name of an enum value", or something similar. – Rufus L Oct 24 '20 at 01:07
  • @RufusL to be more specific what I would need it's to get the numeric value from an enum using the string name, yeah, I'm not sure why was it done this way, I posted the general problem because maybe there's an easy way to do the whole. But spliting the string, creating an array and iterating over it then use each name to get the numeric value would work – José Luis Flores García Oct 24 '20 at 01:12

2 Answers2

0

Okay Provided what you told @Rufus L, there is an easy solution

using System;
                
public class Program
{
    public static void Main()
    {
        Console.WriteLine(((int)Name.TEST).ToString());
        Console.WriteLine(((short)Name.TEST2).ToString());
        Console.WriteLine(((long)Name.TEST3).ToString());
    }

}
public enum Name
{
    TEST,
    TEST2,
    TEST3
}

Output of that is 0, 1, 2 . I parsed it with different types because maybe you need a larger number.

Im editing my answer just in case because i think thats not exactly what you need, but it may come in handy, so i leave it and also i ll add this, if you want to transform a literal string into an enum value, you would need to parse it like this

var enumVal = (Name)Enum.Parse(typeof(Name), "TEST");

And then you can apply what i wrote earlier

nalnpir
  • 1,167
  • 6
  • 14
0

Assuming that your example2 enum names are the same as their unshifted example names:

var ex = example2.name1 | example2.name2;
    
var list = 
    Enum.GetValues(typeof(example2))
       .OfType<example2>()
       .Skip(1)
       .Where(x => (ex & x) == x)
       .Cast<int>()
       .Select(x => Math.Log(x,2));

Console.WriteLine(string.Join(",", list); 

Produces

1, 2

And you can convert those to strings if you wish. i.e. you can accomplish this without creating strings and without parsing them back into ints.

Ian Mercer
  • 38,490
  • 8
  • 97
  • 133
  • Why not simply `Console.WriteLine(ex)`; this also returns "name1, name2"? But this was anyhow not the result expected in the question. – Klaus Gütter Oct 24 '20 at 05:00
  • @KlausGütter that doesn't get you a list of strings, that gets you a single string. This shows how to get the component enum values as a list. I updated it to show how to convert those values to int values as in the original enum. OK? – Ian Mercer Oct 24 '20 at 07:18
  • Thanks, I will just change it to a list of strings and it'll be done. – José Luis Flores García Oct 26 '20 at 14:58