Question:
C# error: Cannot implicitly convert type 'bool' to 'string'?
papasmurff117
2009-07-28 19:01:22 UTC
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
switch (data)
{
case data.Substring(0,6) == "send /":
MessageBox.Show(data.Substring(7));
break;
}

Error: Cannot implicitly convert type 'bool' to 'string'

This comes from the case data.Substring(0,6) =="send /":

Does anyone know how i fix this?
Five answers:
llaffer
2009-07-28 19:26:12 UTC
VB is more linient on casting variable types than C#. C# is actually one of the most rigid.



You can't have an expression on the case line. It has to be a constant.



So sounds like switch may not be what you want, but an if statement instead, where you can have expressions on both sides of the equals:
The Phlebob
2009-07-28 19:12:14 UTC
I don't think you can use a boolean expression (data.Substring(0,6) == "send /") in a case statement.



Hope that helps.
2009-07-28 19:09:54 UTC
Wow... this code is terrible... what exactly are you trying to do?



A switch/case format is like this:

switch ()

{

case :

}



What you're trying to do looks more like it should be an if condition than a switch statement.
tatis
2016-10-14 08:28:32 UTC
First of, in C# an equals assessment is ==, no longer = Secondly, it possibly potential you are trying to benefit 2 values of categories. you ought to explicitly convert them first utilising (int) or (bool)
2009-07-28 19:09:49 UTC
Try



switch (data.Substring(0,6))

{

case "send /":


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...