Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

531
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!