-2

my enum :

public enum UNH
        {
            Message_Reference_Identifier = 0,
            Message_Type = 1,
            Message_version_number = 2,
            Message_release_number = 3,
            Controlling_agency = 4,
            Association_assigned_code = 5
        }

my code line :

int tagcount = Enum.GetNames(typeof(UNH)).Length;

My question is how can I pass UNH as a parameter to typeof(), where UNH will be stored in a string variable.

Hello all, I have multiple enums let's say ABC, DEF, GHI. I would be receiving a string as input parameter, the string would be something like this "ABC+anythinghere", "DEF+anythinghere" and so on. So from here depending on first 3 characters of string parameter i.e. ABC, DEF.... I need to call enum properties which are of same name.

Example if first 3 chars are ABC then there would be n enum values for it similarly if first 3 chars are DEF then there would be m enum values for it.

This first 3 chars would be retrieved from the input string parameter in the form of string let's say testname, when passing the string name into typeof() like typeof(testname) it would obviously consider the variable as string which is not required instead I need a way where the value of testname i.e ABC, DEF.... would be passed into typeof()

  • 4
    This question isn't making much sense to me. What exactly are you trying to achieve here? This sounds a lot like an [XP problem](https://xyproblem.info/). – DavidG Aug 09 '21 at 08:27
  • https://stackoverflow.com/questions/15108786/typeof-how-to-get-type-from-string ? but yeah...why do you need to store the enum type in a string exactly? – ADyson Aug 09 '21 at 08:27
  • You can pass `typeof(UNH)` as parameter (of type `Type`). – Klaus Gütter Aug 09 '21 at 08:29
  • Have you ever heard about "code decoration"? Use frienly names, as has been showed here: [How to decorate enum with attribute in c#](https://stackoverflow.com/questions/40153964/how-to-decorate-enum-with-attribute-in-c-sharp). You don't need function to use the name of enum value ;) – Maciej Los Aug 09 '21 at 08:33
  • Hello all, I have multiple enums let's say ABC, DEF, GHI. I would be receiving a string as input parameter, the string would be something like this "ABC+anythinghere", "DEF+anythinghere" and so on. So from here depending on first 3 characters of string parameter i.e. ABC, DEF.... I need to call enum properties which are of same name. Adding further part in question.. – Rajas Pawaskar Aug 09 '21 at 09:07
  • @RajasPawaskar `typeof` has no parameter. It's not really a method but a keyword like `switch` and it's more of a language command than a function as used in everyday coding. Do you ask for `string name = UNH.Message_Type.ToString();` and `UNH.Message_Type.ToString() + "," + UNH.Controlling_agency.ToString()` ? Or do you want to parse a string "EnumValue1+EnumValue2" to get splitted items on "+" converted to enum values as a list to process them ? Reading your last comment, is that last thing ? But you need to know the enum type like "Type1.Value1+Type2.Value2" unless you act on only one enum. –  Aug 09 '21 at 10:42
  • https://stackoverflow.com/questions/15108786/typeof-how-to-get-type-from-string –  Aug 09 '21 at 10:46

1 Answers1

1

Maybe I misunderstood the question, but this may help:

Enum.GetName:

public class Example
{
    public enum Days
    {
        Monday = 1,
        Tuesday = 2,
        Wednesday = 3,
        Thursday = 4,
        Friday = 5,
        Saturday = 6,
        Sunday = 7
    }
 
    public static void Main()
    {
        int value = 5;
 
        string day = Enum.GetName(typeof(Days), value);
        Console.WriteLine(day);
    }
}
 
// Output: Friday

Using casting:

public class Example
{
    public enum Days
    {
        Monday = 1,
        Tuesday = 2,
        Wednesday = 3,
        Thursday = 4,
        Friday = 5,
        Saturday = 6,
        Sunday = 7
    }
 
    public static void Main()
    {
        int value = 5;
 
        var day = (Days)value;
        Console.WriteLine(day);
    }
}
 
/*
    Output: Friday
*/

Hope this help you to solve a problem you got!