# What happens to Select Case \<string\> ?

**URL:** <https://forum.xojo.com/t/what-happens-to-select-case-string/20442>\
**Category:** General\
**Created:** [November 20, 2014, 9:22am UTC](https://forum.xojo.com/t/what-happens-to-select-case-string/20442 "2014-11-20T09:22:35Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [November 20, 2014, 9:22am UTC](https://forum.xojo.com/t/what-happens-to-select-case-string/20442/1 "2014-11-20T09:22:35Z")

</div>

I have:

[code]Dim FirstName As String

FirstName = “Emile”

Select Case FirstName[/code]

etc.

and at run time I get:

```auto
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

---

<div class="post-metadata">

**Author:** ![Roger\_Clary](https://forum.xojo.com/letter_avatar_proxy/v4/letter/r/eada6e/32.png) [@Roger\_Clary](https://forum.xojo.com/u/Roger_Clary)\
**Post date:** [November 20, 2014, 9:47am UTC](https://forum.xojo.com/t/what-happens-to-select-case-string/20442/2 "2014-11-20T09:47:02Z")

</div>

Looks ok so far. How do you compare your case statements? Do you “End” the Select Case block?

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [November 20, 2014, 9:59am UTC](https://forum.xojo.com/t/what-happens-to-select-case-string/20442/3 "2014-11-20T09:59:05Z")

</div>

[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" `

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [November 20, 2014, 11:28am UTC](https://forum.xojo.com/t/what-happens-to-select-case-string/20442/4 "2014-11-20T11:28:20Z")

</div>

> [@144638:@Roger Clary](#):
>
> Looks ok so far. How do you compare your case statements? Do you “End” the Select Case block?

[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.

[Select Case](http://documentation.xojo.com/index.php/Select).

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.

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [November 20, 2014, 11:30am UTC](https://forum.xojo.com/t/what-happens-to-select-case-string/20442/5 "2014-11-20T11:30:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jeremie\_L](https://forum.xojo.com/user_avatar/forum.xojo.com/jeremie_l/32/150_2.png) [@Jeremie\_L](https://forum.xojo.com/u/Jeremie_L)\
**Post date:** [November 20, 2014, 12:35pm UTC](https://forum.xojo.com/t/what-happens-to-select-case-string/20442/6 "2014-11-20T12:35:35Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [November 20, 2014, 12:49pm UTC](https://forum.xojo.com/t/what-happens-to-select-case-string/20442/7 "2014-11-20T12:49:56Z")

</div>

Thank you Jrmie.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [November 20, 2014, 1:01pm UTC](https://forum.xojo.com/t/what-happens-to-select-case-string/20442/8 "2014-11-20T13:01:25Z")

</div>

Another option:

```auto
select case true
case FirstName = "Emile"
case FirstName.Right( 1 ) = "e"

```

---

<div class="post-metadata">

**Author:** ![Richard\_Duke](https://forum.xojo.com/user_avatar/forum.xojo.com/richard_duke/32/246_2.png) [@Richard\_Duke](https://forum.xojo.com/u/Richard_Duke)\
**Post date:** [November 20, 2014, 1:28pm UTC](https://forum.xojo.com/t/what-happens-to-select-case-string/20442/9 "2014-11-20T13:28:58Z")

</div>

[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…

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [November 21, 2014, 9:52am UTC](https://forum.xojo.com/t/what-happens-to-select-case-string/20442/10 "2014-11-21T09:52:47Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![Jeremie\_L](https://forum.xojo.com/user_avatar/forum.xojo.com/jeremie_l/32/150_2.png) [@Jeremie\_L](https://forum.xojo.com/u/Jeremie_L)\
**Post date:** [November 21, 2014, 12:11pm UTC](https://forum.xojo.com/t/what-happens-to-select-case-string/20442/11 "2014-11-21T12:11:23Z")

</div>

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`

```auto
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.

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [November 21, 2014, 12:30pm UTC](https://forum.xojo.com/t/what-happens-to-select-case-string/20442/12 "2014-11-21T12:30:44Z")

</div>

Good point.

---

<div class="post-metadata">

**Author:** ![Oliver\_Scott-Brown](https://forum.xojo.com/letter_avatar_proxy/v4/letter/o/8baadc/32.png) [@Oliver\_Scott-Brown](https://forum.xojo.com/u/Oliver_Scott-Brown)\
**Post date:** [November 23, 2014, 1:37pm UTC](https://forum.xojo.com/t/what-happens-to-select-case-string/20442/13 "2014-11-23T13:37:16Z")

</div>

[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`

```auto
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.

---

<div class="post-metadata">

**Author:** ![Oliver\_Scott-Brown](https://forum.xojo.com/letter_avatar_proxy/v4/letter/o/8baadc/32.png) [@Oliver\_Scott-Brown](https://forum.xojo.com/u/Oliver_Scott-Brown)\
**Post date:** [November 23, 2014, 1:40pm UTC](https://forum.xojo.com/t/what-happens-to-select-case-string/20442/14 "2014-11-23T13:40:27Z")

</div>

```auto
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:

```auto
dim evaluateStringToTo as string = evaluateString("toto")
If evaluateStringToTo= "123" then
//Do something
Elseif evaluateStringToTo = "456" then
//Do something else
End If
```

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [November 23, 2014, 2:07pm UTC](https://forum.xojo.com/t/what-happens-to-select-case-string/20442/15 "2014-11-23T14:07:50Z")

</div>

> [@145302:@Oliver Scott-Brown](#):
>
> how easy the code is to read and write.

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 ☹

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 5:29pm UTC](https://forum.xojo.com/t/what-happens-to-select-case-string/20442/16 "2020-10-29T17:29:50Z")

</div>


