Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi, Is there a simpler way to do 1

Status
Not open for further replies.

robdon

Programmer
May 21, 2001
252
0
0
ES
Hi,

Is there a simpler way to do the following....

If intX = 3 or intX = 6 or intX = 45 or intX = 54 or intX = 67 or intX = 79 or intX = 102 or intX = 106 or intX = 109 then

I have many values that I want to test for and do the same thing for. I would like to specify them like say in a 'neater' way if possible... IE..

If intX = 3,6,45,54,67,79,102,106,109 then.....

Is there a clearer and easier way or do I have to do it the way I have done it at the top?

Thanks for any suggestions,

Rob Donovan
 
Select Case intX
Case 3, 6, 45, 54, 67, 79, 102, 106, 109
***** PLACE CODE HERE *****
Case Else: ***** PLACE CODE HERE *****
End Select
 
store values in an array like this
arrNums(1)= 6
arrNums(2)= 12
arrNums(3) = 17

then use a loop to check them

for i = 0 to ubound(arrNums)
if intX = arrNums(i) then
'do what you want to do
'exit the for loop
end if
next i

hope this helps
Dawn

 
Hi,

Thanks for the answers guys, that will be fine :)

Rob D
 
I would also go with SWI's solution. The thought behind my answer was if he was getting the values from someplace or if they change, he could load them into an array. If they are hardcoded then I agree the select case is the best way to go.
Dawn
 
Just to point out that you can create the array dynamically and performance is not too bad

dim v as variant, blnTrue as Boolean

for each v in array(3, 6, 45, 54, 67, 79, 102, 106, 109)
if intx = cint(v) then
'do something
blnTrue = True 'remember that we've found one
exit for
next

if not blnTrue then 'do something else

As morningstarr pointed out, then contents of the array might need to change, but I guess it was only a simple question

Mark
 
I too vote for Swi's method, provided that the exception values are known at design time.

If however, the exception values and determined thru the logic of the program, or user inputs, etc ..., using the case statement becomes a little more cumbersome.

In this case, I might opt for the array method, or perhaps a keyed collection approach.

Good Luck


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top