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