0

I've got this enum:

namespace MyApp.Internal.Enums
{
    public enum EServerType
    {
        API
    }
}

I am trying to use the Factory pattern to get a class according to a string, and this is my factory (with both the working code and the error):

using MyApp.Handlers.Interfaces;
using MyApp.Internal.Enums;
using MyApp.Services.Interfaces;

namespace MyApp.Handlers
{
    public class ServerCallerFactory : IServerCallerFactory
    {
        public IServerCaller GetServerCaller(string serverType)
        {
            if (serverType.ToUpper() == EServerType.API.ToString())
            {

            }

            switch (serverType.ToUpper())
            {
                case EServerType.API.ToString():
                    break;
                default:
                    break;
            }
        }
    }
}

I prefer switching over the string instead of using multiple if statements of course.
The if block isn't throwing any compiler error, however, the switch block is!

Error thrown by compiler: CS0426 The type name 'API' does not exist in the type EServerType.

Why is this happening and how to fix it?

Paul Karam
  • 4,052
  • 8
  • 30
  • 53
  • 1
    I think, there are two same name EServerType enum declared in different namespace of your project, is it right ? If it is right, you must use full name of enum. For example: MyApp.Internal.Enums.EServerType.API.ToString() ... – Ramil Aliyev 007 Feb 13 '21 at 19:06
  • Which version of C# are you using? Aside: `string.Equals(..., StringComparison.OrdinalIgnoreCase)` is more efficient than `ToUpper` – Charlieface Feb 13 '21 at 19:09
  • 1
    @RamilAliyev I've already checked that and there's no duplicate enums. Also, that doesn't explain why the `if` doesn't throw a compiler error but the `switch` does. – Paul Karam Feb 13 '21 at 19:10
  • @Charlieface I want to use the `switch` block, the `if` is there just to show that it doesn't throw the error.. Which for my little experience is weird. If I am not mistaken, my C# version should be C# 8.0 – Paul Karam Feb 13 '21 at 19:11
  • 4
    It's a nonconstant expression, so you need to use pattern-matching to do this `case var s when (s == EServerType.API.ToString())` Why don't you just use `Enum.TryParse` it has a case-insensitive version – Charlieface Feb 13 '21 at 19:23
  • The code you posted is illegal. You can't call `ToString()` in an enum's `case` statement like that. Per the previous comment, you could change to the more dynamic pattern-matching syntax, but since what you really want to do is identify the enum value that corresponds to the string being given, you just need to parse the enum. See duplicate. – Peter Duniho Feb 13 '21 at 19:26
  • @Charlieface Thank you.. I've actually used the Enum.TryParse in another piece of code, but it wasn't switch statement. I didn't think of using it here.. I'll go with that probably. – Paul Karam Feb 13 '21 at 19:29
  • @PeterDuniho I guess working with VB.net for the past few years made me think that I can do that in C#... Thank you.. I will go with the TryParse and switch over the Enum type itself. – Paul Karam Feb 13 '21 at 19:30
  • 1
    You can use NameOf, because value of ToString is not known at compile time and case should be a constant value – Vivek Nuna Feb 13 '21 at 19:33
  • 1
    @PaulKaram case requires const value, ToString() is working runtime in your code. Therefore you can use nameof expression, that nameof is working compile time. Change your code like as below, it will working: `switch (serverType.ToUpper()){ case nameof(EServerType.API): break; default: break; }` – Ramil Aliyev 007 Feb 14 '21 at 07:46

0 Answers0