Xyz
Xyz

Reputation: 393

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
}

Upvotes: 3

Views: 6390

Answers (6)

amit-agrawal
amit-agrawal

Reputation: 1603

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

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)
    { ... }

Upvotes: 3

Shaul Behr
Shaul Behr

Reputation: 38033

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

You should change your string into an Enum:

switch ((UserType)Enum.Parse(usertype, typeof(UserType))) {
  ...
}

Upvotes: 5

Sani Huttunen
Sani Huttunen

Reputation: 24385

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;
  }
}

Upvotes: 5

Klaw
Klaw

Reputation: 7887

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)

Upvotes: 0

user31571
user31571

Reputation: 65

You can't compare a String with an Enum.

You should pass an Enum to your method.

Upvotes: 0

Cerebrus
Cerebrus

Reputation: 25775

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;
  }
}

Upvotes: 2

Related Questions