Select/Case and Array()

is there a way to use an array statment as the argument of CASE?

Dim names() As Text
names = Array("Bob", "Brenda", "Betty", "Ben")

select case userName
case in names
msgbox "good"
case else
msgbox "no good"
end select

something like this

I have a few diffeent select statements with in each one of the CASE statements is a list of 14 items
and I’d like to NOT have 14 “case” statements, plus if need be I can alter the array in once place

[quote=271874:@jean-paul devulder]one soluce,

case names(0),names(1),names(2),names(3)[/quote]
doesn’t help if I need to do it in multiple places, and may change the contents of the array over time
but thanks

solution
Create a function that return true/false if parameter is in the “array”

select case username
case inArray(username)
msgbox "good"
case else
msgbox "bad"
end select

select case is all about boolean expressions

Dim names() As Text
names = Array("Bob", "Brenda", "Betty", "Ben")

select case TRUE
case names.IndexOf( userName ) > -1
msgbox "good"
case else
msgbox "no good"
end select

but its just as easy to write it with an if

While I like Select Case, for this situation I routinely just do:

If StrArr.Indexof("bob") > -1 Then ' Else ' End if

BTW your code would need to read:

[code]select case True
case inArray1(username)
msgbox “good”

case inArray2(username)
msgbox “good”
else
msgbox “bad”
end select[/code]

( I assume you want to test against multiple arrays - I don;t see the point of select case for just one)

But i could see being able to do

[code]Select Case DataType
Case In Array1OfDatatype

Case In Array2OfDatatype

Case In Array3OfDatatype

Else

End Select[/code]

Along with the other regular Select case stuff would be useful… as well as very readable and it seems like something that Xojo should be able do…

Maybe worth a feature request?

  • Karen

That got me thinking…

It would be more readable/elegant/intuitive to be able to do:

If Datatype In ArrayOfDatatype Then

Rather than

If ArrayOfDataType.IndexOf(DataType) >-1 then

  • Karen

Not sure you mean “datatype”
Maybe something like

Dim names() As Text
names = Array("Bob", "Brenda", "Betty", "Ben")

select case true
case userName in Names()
  msgbox "good"
case else
  msgbox "no good"
end select

and you could check to see if the value is in one or more arrays and IN would be shorthand for “array.indexOf( value ) > -1”

[quote=271880:@Karen Atkocius]
It would be more readable/elegant/intuitive to be able to do:

If Datatype In ArrayOfDatatype Then

Rather than

If ArrayOfDataType.IndexOf(DataType) >-1 then

  • Karen[/quote]

When I read this I see wanting to do

If Integer In ArrayOfDatatype Then

which is why I question “datatype” and I think you mean

if <value of type T> in <array of type T>

ie/

dim intValue as integer
dim listOfInts() as integer

if  intValue in listOfInts

To be clear I’m taking about a potential feature request, not currently working code.

What i meant by datatype is a variable or literal of the same datatype as the array (though CType might be be needed with numeric literals…(or maybe not depending on how to was implemented )

But I usually use select case with variables (so Type is known) and I suspect that is true for most In any case it would only matter for In Array.

So If X is an integer perhaps passed into a method, and IntArrayA and IntArrayB ware integer arrays with some values this is how I would like to see it working:

Select Case X Case In IntArrayA 'Do this Case 99 'Do this Case 77, In IntArrayB ' Do this Else ' Do that End Select

For Strings:

[code]names = Array(“Bob”, “Brenda”, “Betty”, “Ben”)

select case “Ben”
case In Names, “Todd”
msgbox “good”
case else
msgbox “no good”
end select
[/code]

That makes it much more flexible than Select case True as well as much more readable and intuitive.

So for an If statement it would be

If X In IntArrayA Then ' Do this Else ' Do that End if

Dim B as Boolean = (X In IntArrayA)

Basically I am talking about an “In” operator that returns a boolean result that also gets built into the Select Case as “IsA” was .

  • Karen