0

I have an Enum of different AppTypes.

public enum AppType
{
    DefaultApps = 1,
    GlobalApps = 3,
    PrivateApps = 'L'
}

I will receive a string of form "1", "3" or "L" and want to use this in switch statement. I am trying to do as below. but not working. am I missing anything.

switch (appTypeString)
{
  case $"{(char)AppType.DefaultApps}:": // also tried nameof(AppType.GlobalApps)
    Console.WriteLine("Default Catalog Apps");
    break;
  case nameof(AppType.GlobalApps):
    Console.WriteLine("Global Catalog Apps");
    break;
  case nameof(AppType.PrivateApps):
    Console.WriteLine("Private Catalog Apps");
    break;
  default:
    Console.WriteLine("Unknown App Type");
    break;
}
Programmerzzz
  • 1,237
  • 21
  • 48
  • 1
    Assuming that `appTypeString` is actually a string... When you've tried this code, what values has it had? – madreflection Jun 12 '23 at 15:50
  • 1
    Side note: you're using the enum for its member names in string form, not as an actual enum. You might as well define them as `const` strings and lose the `enum`. – madreflection Jun 12 '23 at 15:51
  • Yes, the `nameof(AppType.DefaultApps)` has `DefaultApps`. Yes, I did define 3 more constants as string and using them in thsi Switch case, but I am being pushed to use Enum here as well which I am having issues with – Programmerzzz Jun 12 '23 at 15:53
  • *"pushed to use enum"* by whom? And why didn't they explain how to do that properly? – madreflection Jun 12 '23 at 15:54
  • hahha, pushed by the team, I was wondering if I dont know how to use this in enum and was investigating on how to do it. couldnt get that working – Programmerzzz Jun 12 '23 at 15:55
  • 1
    Does this answer your question? [C# how to use enum with switch](https://stackoverflow.com/questions/15136134/c-sharp-how-to-use-enum-with-switch) – Soumen Mukherjee Jun 12 '23 at 15:55
  • 1
    Also, enums are numeric, which is why using them solely for their member names as strings is missing the point. Member `PrivateApps` will have a value of 76, the code point of the `'L'` character. If the user can enter '1', '3', or 'L', you have a mix of basic number values (1, 3) and code points (76). – madreflection Jun 12 '23 at 15:56
  • 3
    Use `Enum.Parse` to convert your string to an enum. Then you can switch on the enum. – Matt Johnson-Pint Jun 12 '23 at 15:58
  • Just cast the value to the enum and then use the enum in the switch. Example `(AppType)'L'` – Ralf Jun 12 '23 at 16:02

2 Answers2

2

You can do like this:

AppType test = AppType.DefaultApps;
switch (test)
{
    case AppType.DefaultApps: 
        Console.WriteLine("Default Catalog Apps");
        break;
    case AppType.GlobalApps:
        Console.WriteLine("Global Catalog Apps");
        break;
    case AppType.PrivateApps:
        Console.WriteLine("Private Catalog Apps");
        break;
    default:
        Console.WriteLine("Unknown App Type");
        break;
}

In your case you have a string as the Input, I would do like this:

string val = "L";
AppType test;
bool flag = Enum.TryParse(val, out test);
//Since AppType.PrivateApps is of type char it will fail the Enum.TryParse() so we make a conversion
if (!flag)
{
    if(val.Length == 1)
        test = (AppType)(val.ToCharArray().First());
}
switch (test)
{
    case AppType.DefaultApps: 
        Console.WriteLine("Default Catalog Apps");
        break;
    case AppType.GlobalApps:
        Console.WriteLine("Global Catalog Apps");
        break;
    case AppType.PrivateApps:
        Console.WriteLine("Private Catalog Apps");
        break;
    default:
        Console.WriteLine("Unknown App Type");
        break;
}
Nathan
  • 83
  • 5
  • here, the input to Switch case is from a string like in my example. can you please elaborate? – Programmerzzz Jun 12 '23 at 15:57
  • If the argument to the `switch` is a string, you're not using an enum. At all. You *have* an enum, but you're not using it. – madreflection Jun 12 '23 at 15:59
  • @Programmerzzz Here I elaborated! – Nathan Jun 12 '23 at 16:08
  • 1
    [`Enum.Parse(String)`](https://learn.microsoft.com/en-us/dotnet/api/system.enum.parse?view=net-7.0#system-enum-parse-1(system-string)) has been around since .NET Core 2.0, and [`Enum.TryParse(String, TEnum))`](https://learn.microsoft.com/en-us/dotnet/api/system.enum.tryparse?view=netframework-4.0#system-enum-tryparse-1(system-string-0@)) has been around since .NET Framework 4, so I'd use those instead of the untyped `Enum.Parse()` methods. – dbc Jun 12 '23 at 16:13
  • It will fail on "L" but shouldn't. – Ralf Jun 12 '23 at 16:14
  • Good points, any idea how to handle this error? – Nathan Jun 12 '23 at 16:20
0

You can also use the new switch expression:

var toPrint = appTypeString switch
  {
      AppType.DefaultApps => "Default Catalog Apps",
      AppType.GlobalApps => "Global Catalog Apps",
      AppType.PrivateApps => "Private Catalog Apps",
      _ => "Unknown App Type",
  };

Console.WriteLine(toPrint);
Charlieface
  • 52,284
  • 6
  • 19
  • 43