-1

I'm trying to compare the enum element with the database value; Comparing the names(Value) works fine but the names are subjects to change so i have to compare by IDs which is why i assigned numbers to each enum element;

Testing the comparation includes getting the random element from the list of some(not all) string extension values of said enum.

Is there any way to get the assigned number of the element or at least index after random variable(randomElemnt) is set?

I tried to create another variable

var randomElemntId = (ProductName)Enum.Parse(typeof(ProductName),randomElemnt);

Casting to an int didn't yield desirable result either.

This is my example enum with string extension Value

//Value is a string extension
public enum ProductName
    {
        [Value("Product1")] Product1 = 2,
        [Value("Product2")] Product2 = 3,
        [Value("Product3")] Product3 = 4,
        [Value("Product4")] Product4 = 1,
        [Value("None")] None = 0
    }

Example of a list that i use. Note that not all of the enum elements are present; Variable which gets random element of the list

//contains some enum values
     var Products = new List<string>
            {
                ProductName.Product1.GetValue(),
                ProductName.Product2.GetValue(),
                ProductName.Product3.GetValue(),

            };

//returns random element from the list
var randomElemnt = Products.RandomElement();

Maledictum
  • 27
  • 5
  • 1
    Possible duplicate of [Convert a string to an enum in C#](https://stackoverflow.com/questions/16100/convert-a-string-to-an-enum-in-c-sharp) – Sinatr May 17 '19 at 13:50
  • "Casting to an int didn't yield desirable result either." -- what did you cast to an int? The string, or the enum value? `(int)ProductName.Product3` will return `4`. – 15ee8f99-57ff-4f92-890c-b56153 May 17 '19 at 14:23
  • @EdPlunkett Indeed, casting the enum value gave me the int needed. The problem is, i have to operate with the string variable(randomElmnt) which is not an option. I have to rephrase the question a bit. – Maledictum May 17 '19 at 14:42
  • @Maledictum You know how to parse the string value to get the enum. But perhaps a rephrase will clarify. – 15ee8f99-57ff-4f92-890c-b56153 May 17 '19 at 14:43
  • If your code needs to account for names being subject to change then you can't put the names in code unless you generate the code after each change. Some ORMs do that. – Tom Blodget May 17 '19 at 15:06

2 Answers2

1

I found an answer to my question:

First of all, List<string> was bad thing to do in my case so i've created List :

var Products = new List<ProductName>
            {
                ProductName.Product1,
                ProductName.Product2,
                ProductName.Product3,

            };

Then, my variable var randomElemnt = Products.RandomElement(); became non-string enum member After that i was able to extract enum int via another variable var randomElmntId = (int)randomElmnt; While still being able to operate with randomElmnt

Thank you all for your contributions which made me come to this solution.

Maledictum
  • 27
  • 5
0

Code below show how to cast number to string and string to number :

        public enum ProductName
        {
            Product1 = 2,
            Product2 = 3,
            Product3 = 4,
            Product4 = 1,
            None = 0
        }

        static void Main(string[] args)
        {
            Random rand = new Random();
            List<ProductName> products = Enumerable.Range(0,10).Select(x => (ProductName)rand.Next(0,5)).ToList();
            string[] names = products.Select(x => x.ToString()).ToArray();

        }
jdweng
  • 33,250
  • 2
  • 15
  • 20