Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

533
Visualizações
C# - Switch Enum - Type name ' ' does not exist in the type ' '

I am getting an error "The type name 'Home' does not exist in the type 'MenuEnum'" when using it in a switch case. If I just go a if statement it works without issue.

The issue is when I use the MenuEnum.Home I am getting a IDE error and my code will not compile.

I also switched to a regular switch statement below in the code example.

enter image description here

enter image description here

enter image description here

Added Code below

public void Selected(MenuEventArgs<MenuItem> args)
{
    //The ULR to navigate to
    var url = string.Empty;

    try
    {
        //If there is no data do nothing
        if(string.IsNullOrEmpty(args.Item.Text))
            return;
            
        //switch on the incoming text
        switch (args.Item.Text)
        {
            //IDE Error on home (will not compile)...
            case MenuEnum.Home.ToString():
                url = "/";
                break;
            default:
                url = "";
                break;
        }

        //working code
        if (args.Item.Text == MenuEnum.Home.ToString().Replace('_', ' '))
        {
            url = "/";
        }


    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
    
    Navigation.NavigateTo(url);  
}  

/-Enum file-/

 namespace ManagerDashboard2022.Client.ENUMs;

 /// <summary>
 /// The Items in the menu
 /// </summary>
 public enum MenuEnum
 {
     Home
 }
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

If you really want to switch on string values, you can replace that

 case MenuEnum.Home.ToString():

with

 case nameof(MenuEnum.Home):

nameof is evaluated at compile time to result in the string constant "Home" (in this case). As this is a constant, you can "switch" on it.

The advantage of using nameof instead of just the string "Home" is that with nameof the MenuEnum.Home value needs to exist - so you get an compiler error on typos.

That error message you got:

"The type name 'Home' does not exist in the type 'MenuEnum'"

is not very helpful in this case. It should have been (IMO) something like "you cannot have a runtime expression as case label".

over 4 years ago · Santiago Trujillo Relatório

0

Thank you @cly.

I needed to parse the enum and switch on that returned item

public void Selected(MenuEventArgs<MenuItem> args)
{
    //The ULR to navigate to
    var url = string.Empty;

    try
    {
        //If there is no data do nothing
        if(string.IsNullOrEmpty(args.Item.Text))
            return;

        //convert text to enum
        var menuItem = Enum.Parse<MenuEnum>(args.Item.Text);
            
        //switch enum
        url = menuItem switch
        {
            MenuEnum.Home => "/",
            _ => ""

        };
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
    
    Navigation.NavigateTo(url);  
}  
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda