8

Why this swich statement do not work, gives error:

A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type

Code:

 switch (btn.BackColor)
 {
     case Color.Green:
         break;
     case Color.Red:
         break;
     case Color.Gray:
         break;
 }
Jeff Roe
  • 3,147
  • 32
  • 45
NoviceToDotNet
  • 10,387
  • 36
  • 112
  • 166

4 Answers4

8

The error in in itself is self explanatory. it it telling you that switch expression must be ofone of these types: sbyte, byte, short, ushort, int, uint, long, ulong, char, string. or as the C# language specification suggests

exactly one user-defined implicit conversion (§6.4) must exist from the type of the switch expression to one of the following possible governing types: sbyte, byte, short, ushort, int, uint, long, ulong, char, string, or, a nullable type corresponding to one of those types.

And you can see that BackColor is returning your a type here and it is not satisfying any of the above rules, hence the error.

you can do it like this

switch (btn.BackColor.Name)
{
   case "Green":
      break;
   case "Red":
      break;
   case "Gray":
      break;
}
Ehsan
  • 31,833
  • 6
  • 56
  • 65
6

The problem is you can't use a Color in a switch statement. It must be one of the following types, a nullable version of one of the types, or or convertible to one of these types: sbyte, byte, short, ushort, int, uint, long, ulong, char, string

From the C# language spec, 8.7.2:

• Otherwise, exactly one user-defined implicit conversion (§6.4) must exist from the type of the switch expression to one of the following possible governing types: sbyte, byte, short, ushort, int, uint, long, ulong, char, string, or, a nullable type corresponding to one of those types.

In your case, you could work around this by using strings, or just using a set of if/else statements.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
3

You can't switch on BackColor because it's not an integral type.

You can only switch on integer types, enums (which are effectively integer types) and chars and strings.

You will need to find a property of BackCOlor that is unique (such as the Name) and switch on that.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
2

As the other answers have pointed out, System.Drawing.Color is not a usable type in a switch statement. Color is an interesting type because it behaves like an enum in code, but that is because it has a static property for each System.Drawing.KnownColor, which is an enum type. So when you see Color.Green in code, here is what the Color class is doing behind the scenes:

public static Color Green
{
  get
  {
    return new Color(KnownColor.Green);
  }
}

Knowing this bit of information, you can write your code like this to use the BackColor property in a switch:

if (btn.BackColor.IsKnownColor)
{
    switch (btn.BackColor.ToKnownColor())
    {
        case KnownColor.Green:
            break;
        case KnownColor.Red:
            break;
        case KnownColor.Gray:
            break;
    }
}
else
{
    // this would act as catch-all "default" for the switch since the BackColor isn't a predefined "Color"
}
avanek
  • 1,649
  • 12
  • 20