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.
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
}
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".