views:

789

answers:

6
+2  Q: 

Enum & Switch Case

Hi I am using enums converted to a string with a switch but it doesn't work. It gives compilation error: Cannot implicitly convert type 'userControl_commontop.UserType' to 'string'

The code is:

private void CommonTopChangesnew(string usertype)
{

    switch (usertype.Trim().ToUpper())
    {
        case UserType.NORMAL :
            hlkSAD.Enabled = false;
            hlkMRTQuery.Enabled = false;
            hlkReqViewer.Enabled = false;
            hlkSendnotif.Enabled = false;
            break;
        case UserType.POWER :
            hlkSAD.Enabled = false;
            hlkReqViewer.Enabled = false;
            hlkSendnotif.Enabled = false;
            break;
    }
}

enum UserType
{
    NORMAL,
    POWER,
    ADMINISTRATOR
}
+5  A: 

The enumeration is not a string, any more than a constant const int MY_VALUE = 1; is a string.

If you want to convert the enum values into a string, do:

case UserType.NORMAL.ToString():

etc.

Alternatively, you could change your string into an Enum:

switch ((UserType)Enum.Parse(usertype, typeof(UserType))) {
  ...
}
Shaul
The first option probably won't work as switch requires case statements to be constant.
Josh Einstein
+3  A: 

You should try this:

enum UserType
{
  NORMAL,
  POWER,
  ADMINISTRATOR
}

private void CommonTopChangesnew(string usertype)
{
  switch ((UserType)Enum.Parse(typeof(UserType), usertype, true))
  {
    case UserType.NORMAL:
      hlkSAD.Enabled = false;
      hlkMRTQuery.Enabled = false;
      hlkReqViewer.Enabled = false;
      hlkSendnotif.Enabled = false;
      break;
    case UserType.POWER:
      hlkSAD.Enabled = false;
      hlkReqViewer.Enabled = false;
      hlkSendnotif.Enabled = false;
      break;
  }
}
Sani Huttunen
+1  A: 

Your function accepts a parameter of type string and then you use the same parameter to compare types belonging the Enum. Here lies the conflict.

Your function should instead be :

private void CommonTopChangesnew(UserType usertype)
{

  switch (usertype)
  {
    case UserType.NORMAL :
      hlkSAD.Enabled = false;
      hlkMRTQuery.Enabled = false;
      hlkReqViewer.Enabled = false;
      hlkSendnotif.Enabled = false;
      break;
    case UserType.POWER :
      hlkSAD.Enabled = false;
      hlkReqViewer.Enabled = false;
      hlkSendnotif.Enabled = false;
      break;
  }
}
Cerebrus
A: 

You can't compare a String with an Enum.

You should pass an Enum to your method.

A: 

Option 1: Change your CommonTopChangesnew to accept a UserType enum as a parameter

or

Option 2: Use Enum.Parse to convert your string to a UserType enum in your switch block:

(UserType)Enum.Parse(typeof(UserType), usertype)

Klaw
+2  A: 

You could convert the userType parameter in to an enum value using this funtion:

object Enum.Parse(System.Type enumType, string value, bool ignoreCase);

as

UserType utEnum = Enum.Parse(UserType , userType, true);

and then you can call your switch statement as:

switch (utEnum) { ... }

amit-agrawal