I have:
[code]Dim FirstName As String
FirstName = “Emile”
Select Case FirstName[/code]
etc.
and at run time I get:
Type mismatch error. Expected String, but got boolean.
Why ?
I quit Xojo and reload it to the same error.
Xojo 2014r2.1
Os X 10.9.5
I have:
[code]Dim FirstName As String
FirstName = “Emile”
Select Case FirstName[/code]
etc.
and at run time I get:
Type mismatch error. Expected String, but got boolean.
Why ?
I quit Xojo and reload it to the same error.
Xojo 2014r2.1
Os X 10.9.5
Looks ok so far. How do you compare your case statements? Do you “End” the Select Case block?
[quote=144629:@Emile Schwarz]and at run time I get:
Type mismatch error. Expected String, but got boolean.
Why ?
[/quote]
You should post the entire code. I bet you did something like :
Dim FirstName As String
FirstName = "Emile"
Select Case FirstName
case FirstName = "Emile" // This is a boolean
What you want to do is :
Dim FirstName As String
FirstName = "Emile"
Select Case FirstName
case "Emile"
[code]Dim FirstName As String
FirstName = “Emile”
Select Case FirstName
Case “Emile”
// Do something
Case “Albert”
// Do something
Case “Roger”
// Do something
Case Right(“Emile”,1) = “e”
// Do something different
Case InStr(FirstName,“S”)
// Do something different
Else
// Not found
End Select[/code]
Sorry, I was thinking the shared code was enough since the error goes to Select Case testExpression
Or
I may forgot to say something important.
OK: because both answers ask for more data, I tried some more tests and found working cases:
Case “1”
Case “August”
Compiles.
The following produce the error message
Case FirstName = “Emile”
Case Right(FirstName,1) = “e”
OK (BIS): I now understand the error. In both cases above, the result of the evaluation is a Boolean !
Thanks, I got it. It was 5:30 (on a 24 Hours Clock) when I wrote that. I replaced the Select Case Block by an If ElseIf Block with many empty lines and it is as clear as a Select Case and works fine !
I wasted one hour on this simple error.
BTW: is the IDE report cautionable ?
(it shows the error in the Select Case line instead of in the Case line(s))
PS: I checked on a C++ for Dummies book switch(expression) and found nothing except a ‘bug’ in the 2002 edition: they used in the example ‘cas’ instead of ‘case’. Certainly a translation error.
Roger, Michel:
Thank you for your answers. Roger says that Select Case works and Michel also ask for code; this leads me to think different and found where the error lies (my error).
You could also write it as a combination of Select Case and If Then:
[code]Dim FirstName As String
FirstName = “Emile”
Select Case FirstName
Case “Emile”
// Do something
Case “Albert”
// Do something
Case “Roger”
// Do something
Else
If Right(Firstname,1) = “e”
// Do something different
elseif InStr(FirstName,“S”)>0
// Do something different
Else
// Not found
End If
End Select[/code]
Thank you Jrmie.
Another option:
select case true
case FirstName = "Emile"
case FirstName.Right( 1 ) = "e"
[quote=144664:@Kem Tekinay]Another option:
select case true
case FirstName = "Emile"
case FirstName.Right( 1 ) = "e"
[/quote]
Kem, have no idea you can do that…
Me too !!!
Worst, it took me times to understand what I read: Select Case True My brain escaped True !!!
Nota: I choosed Select Case because its reading is far better than an If / End If block
But after adding extra spaces in the Select Case converted to If / End If code, it happens that the new code is as readable as the previous one.
Another question: do we know if there is a difference between these two ways of doing things ( Select Case vs If / End If ) in speed ?
Regarding speed it depends actually.
Select Case is quicker when you evaluate a function but If/End If is quicker if you evaluate a property.
For example:
Function evaluateString(str As String)
//Takes 100ms to evaluate
Return str
End Function
If evaluateString("toto") = "123" then
//Do something
Elseif evaluateString("toto") = "456"
//Do something else
End If
Select Case evaluateString("toto")
Case "123"
//Do something
Case "345"
//Do something else
End Case
Select Case is quicker because it calls evaluateString only once.
Good point.
[quote=144865:@JrmieLeroy]Regarding speed it depends actually.
Select Case is quicker when you evaluate a function but If/End If is quicker if you evaluate a property.
For example:
Function evaluateString(str As String)
//Takes 100ms to evaluate
Return str
End Function
If evaluateString("toto") = "123" then
//Do something
Elseif evaluateString("toto") = "456"
//Do something else
End If
Select Case evaluateString("toto")
Case "123"
//Do something
Case "345"
//Do something else
End Case
Select Case is quicker because it calls evaluateString only once.[/quote]
But you could store the value of evaluateString in a variable, which would mean that you only have to call it once.
‘select case’ vs ‘if … else’ has an innegligible difference in speed when running. Choosing between the two should depend on how easy the code is to read and write.
If evaluateString("toto") = "123" then
//Do something
Elseif evaluateString("toto") = "456" then
//Do something else
End If
If it is worth the optimization I would personally write the code like this:
dim evaluateStringToTo as string = evaluateString("toto")
If evaluateStringToTo= "123" then
//Do something
Elseif evaluateStringToTo = "456" then
//Do something else
End If
Adding empty lines before each and every ElseIf / Else add clarity / readability to the whole if block and that is what I was searching initially / why I choosed Select Case.
Then, I was wrong in my logic ![]()