Solved

# help with Case statement: is >=

Posted on 2004-04-27
Medium Priority
261 Views
This case statement is not holding the if >= to on the values of the declared doubles...

if dblInc6 is 0 and dblInc1 is 9000, this code will still select this case...what's the deal?

Select Case dblInc6
Case Is >= dblInc1, dblInc2, dblInc3, dblInc4, dblInc5
strEFX = strEFX6
strTUC = strTUC6
strXPN = strXPN6
End Select

Select Case dblInc5
Case Is >= dblInc1, dblInc2, dblInc3, dblInc4, dblInc6
strEFX = strEFX5
strTUC = strTUC5
strXPN = strXPN5
End Select

Select Case dblInc4
Case Is >= dblInc1, dblInc2, dblInc3, dblInc5, dblInc6
strEFX = strEFX4
strTUC = strTUC4
strXPN = strXPN4
End Select

Select Case dblInc3
Case Is >= dblInc1, dblInc2, dblInc4, dblInc5, dblInc6
strEFX = strEFX3
strTUC = strTUC3
strXPN = strXPN3
End Select

Select Case dblInc2
Case Is >= dblInc1, dblInc3, dblInc4, dblInc5, dblInc6
strEFX = strEFX2
strTUC = strTUC2
strXPN = strXPN2
End Select

Select Case dblInc1
Case Is >= dblInc2, dblInc3, dblInc4, dblInc5, dblInc6
strEFX = strEFX1
strTUC = strTUC1
strXPN = strXPN1
End Select
0
Question by:lexo
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 4
• 4
• +4

LVL 3

Expert Comment

ID: 10931647
which language? VB? I don't think you can write those case statements.
by the way, if you use arrays everything will be easier, at least in the code you posted
0

LVL 4

Expert Comment

ID: 10932058
I think there might be a better way for you to do this, but if you want to go with this method I believe the sytnax you need to use is:

Select Case dblInc6
Case Is >= dblInc1, Is >= dblInc2, Is >= dblInc3, Is >= dblInc4, Is >= dblInc5
strEFX = strEFX6
strTUC = strTUC6
strXPN = strXPN6
End Select
0

Author Comment

ID: 10932319
well i'm here for soulutions...feel free to post away...
0

LVL 7

Expert Comment

ID: 10932416
What is dblInc2, dblInc3, dblInc4 and dblInc5...

I believe when you use the >= val1, val2, val3 construct there is an implied OR,
so if the value you are selecting on is greater or equeal to any value in that list
then that case will be the one selected...

Instead of using multiple Select statements this might be clearer:

If dblInc6 >= dblInc1 And dblInc6 >= dblInc2 And dblInc6 >= dblInc3 And dblInc6 >= dblInc4 And dblInc6 >= dblInc5 Then
strEFX = strEFX6
strTUC = strTUC6
strXPN = strXPN6
Else If dblInc5 >= dblInc1 And dblInc5 >= dblInc2 And dblInc5 >= dblInc3 And dblInc5 >= dblInc4 And dblInc5 >= dblInc6 Then
strEFX = strEFX5
strTUC = strTUC5
strXPN = strXPN5
Else If dblInc4 >= dblInc1 And dblInc4 >= dblInc2 And dblInc4 >= dblInc3 And dblInc4 >= dblInc5 And dblInc4 >= dblInc6 Then
strEFX = strEFX4
strTUC = strTUC4
strXPN = strXPN4
Else If dblInc3 >= dblInc1 And dblInc3 >= dblInc2 And dblInc3 >= dblInc4 And dblInc3 >= dblInc5 And dblInc3 >= dblInc6 Then
strEFX = strEFX3
strTUC = strTUC3
strXPN = strXPN3
Else If dblInc2 >= dblInc1 And dblInc2 >= dblInc3 And dblInc2 >= dblInc4 And dblInc2 >= dblInc5 And dblInc2 >= dblInc6 Then
strEFX = strEFX2
strTUC = strTUC2
strXPN = strXPN2
Else If dblInc1 >= dblInc2 And dblInc1 >= dblInc3 And dblInc1 >= dblInc4 And dblInc1 >= dblInc5 And dblInc1 >= dblInc6 Then
strEFX = strEFX1
strTUC = strTUC1
strXPN = strXPN1
End If

Also, if you are trying to see which value is that largest you might consider
something like (VB.Net syntax):

Dim al As ArrayList
al = New ArrayList
al.Sort()

' In the default sort the last object should be the largest.
Select Case al(al.Count - 1)
Case Is val6
' Do something
Case Is val5
' Do something
Case Is val4
' Do something
Case Is val3
' Do something
Case Is val2
' Do something
Case Is val1
' Do something
End Select

Anyhow, just a thought...
0

LVL 2

Accepted Solution

ileanarc earned 2000 total points
ID: 10932989
I would do sth. like:

dim iMax as integer

iMax=dblInc6

'find the maximum
if iMax<=dblInc5 then
iMax = dblInc5
end if

if iMax<=dblInc4 then
iMax=dblInc4
end if

if iMax<=dblInc3 then
iMax=dblInc3
end if

if iMax<=dblInc2 then
iMax=dblInc2
end if

if iMax<=dblInc2 then
iMax=dblInc2
end if

select case  iMax
case is dblInc1
strEFX = strEFX1
strTUC = strTUC1
strXPN = strXPN1
case is dblInc2
strEFX = strEFX2
strTUC = strTUC2
strXPN = strXPN2
case is dblInc3
strEFX = strEFX3
strTUC = strTUC3
strXPN = strXPN3
case is dblInc4
strEFX = strEFX4
strTUC = strTUC4
strXPN = strXPN4
case is dblInc5
strEFX = strEFX5
strTUC = strTUC5
strXPN = strXPN5
case is dblInc6
strEFX = strEFX6
strTUC = strTUC6
strXPN = strXPN6
end select

I think that this way you will have less comparisons and the code will perform better.

Ileana.
0

LVL 4

Expert Comment

ID: 10933076
Well, for instance if you have a list of variables named dblInc1, dblInc2, dblInc3, etc., you can simplify your code by using an array:

Dim dblInc1 as Double, dblInc2 as Double ' etc.
dblInc1 = 1000
dblInc2 = 2000
' etc.

use

Dim dblInc(6) as Double

dblInc(0) = 1000
dblInc(1) = 2000
'etc.

Same for your string variables.  in this case you can use a two-dimensional array.  right now it looks like you have six numbered sets of 3 namesd string variables, strEFX, strTUC and strXPN.  If oyu need to keep them named then fine, they could be threee arrays however you could also use one aray

Dim strMyStringArray(6, 3) As String
strMyStringArray(0, 0) = strEFX1
strMyStringArray(0, 1) = strTUC1
strMyStringArray(0, 2) = strXPN1

and so on.

Further, it looks like the way you are using the numbered string variables, you want them to be associated with the numberd Dbl vairables.  If you are not worried about you data being strongly type, you can simply use a single 6x4 variant array.

Dim vntMyArray(6, 4)
vntMyArray(0, 0) = dblInc1
vntMyArray(0, 1) = strEFX1
vntMyArray(0, 2) = strTUC1
vntMyArray(0, 3) = strXPN1

But if you want to keep your data types specified, then you can use a user defined data type, and stick that in an array:

Private Type MyDT
Inc As Double
EFX As String
TUC As String
XPN As String
End Type

Dim MyArray(6) As MyDT
MyArray(0).Inc = dblInc1
MyArray(0).EFX = strEFX1
MyArray(0).TUC = strTUC1
MyArray(0).XPN = strXPN1

However that can get you into trouble if you need to pass the array as an argument, so you might want to have two arrays, one for the doubles and anothe 2-dim array for the strings.  you can then get the index for the maximum value of the dbl array

x = GetMaxValueIndex(dblInc)
strEFX = strMyStringArray(x, 0)
strTUC = strMyStringArray(x, 1)
strXPN = strMyStringArray(x, 2)
'or whatever

Private Function GetMaxValueIndex(ByRef p_dblArray) As Integer
Dim dblMax As Double
Dim i As Integer
GetMaxValueIndex = LBound(p_dblArray)
dblMax = p_dblArray(GetMaxValueIndex)
For i = GetMaxValueIndex + 1 To UBound(p_dblArray)
If p_dblArray(i) > dblMax Then
GetMaxValueIndex = i
dblMax = p_dblArray(i)
End If
Next
End Function

0

LVL 27

Expert Comment

ID: 10933276
Hi lexo:
I fear you may be confusing the correct use of Select Case.
Select Case is generally used to simplify a complicated IF

Usually we will have multiple Cases within a Select Case.
In your case you have multiple SELECT CASE statements, but each one has only ONE Case within it.

Maybe if you can tell us what you want to accomplish, we might be able to provide you with the correct solution (although there have been quite a few good suggestions already!

As to why "if dblInc6 is 0 and dblInc1 is 9000, this code will still select this case...what's the deal?"
Case Is >= dblInc1, dblInc2, dblInc3, dblInc4, dblInc5
translates (the way I interpret it) to English as:
If dblinc6 is bigger or equal to dblInc1 OR equal to dblInc2 OR equal to dbInc3 ... etc
In your case, dblinc1 may be 9000, but it is enough that one of the other values also is zero for the case to be selected.
Note that the >= will only apply to dblInc1, and you might be looking at lpzCoville's suggestion.

Dabas
0

Author Comment

ID: 10933943
ok ok, let me clarify what I really want to accomplish...
I need to take the highest value from dblInc1 through dblInc6
Then set  strEFX ,  strTUC  AND  strXPN accodingly:

Example: If dblInc5 is the highest value, I need to set
strEFX = strEFX5
strTUC = strTUC5
strXPN = strXPN5

Does this make sense?
0

Author Comment

ID: 10933944
ok ok, let me clarify what I really want to accomplish...
I need to take the highest value from dblInc1 through dblInc6
Then set  strEFX ,  strTUC  AND  strXPN accodingly:

Example: If dblInc5 is the highest value, I need to set
strEFX = strEFX5
strTUC = strTUC5
strXPN = strXPN5

Does this make sense?
0

LVL 4

Expert Comment

ID: 10934004
Dim maxval As Long
maxval = dblInc6
If dblInc5 > maxval Then maxval = dblInc5
If dblInc4 > maxval Then maxval = dblInc4
If dblInc3 > maxval Then maxval = dblInc3
If dblInc2 > maxval Then maxval = dblInc2
If dblInc1 > maxval Then maxval = dblInc1
Select Case maxval
Case dblInc1
strEFX = strEFX1
strTUC = strTUC1
strXPN = strXPN1
Case dblInc2
strEFX = strEFX2
strTUC = strTUC2
strXPN = strXPN2
Case dblInc3
strEFX = strEFX3
strTUC = strTUC3
strXPN = strXPN3
Case dblInc4
strEFX = strEFX4
strTUC = strTUC4
strXPN = strXPN4
Case dblInc5
strEFX = strEFX5
strTUC = strTUC5
strXPN = strXPN5
Case dblInc6
strEFX = strEFX6
strTUC = strTUC6
strXPN = strXPN6
End Select
0

LVL 27

Expert Comment

ID: 10934011
lexo:
There are quite a few solutions to this.
But since lpzCoville's second post is quite close to what you want, I strongly suggest you have a close look at it.
Using arrays will have the extra benefit (amongst others) that if in the future you should need a dblIinc7, or even up to a dblInc1000, the code will hardlly change!

Dabas
0

LVL 2

Expert Comment

ID: 10934601
Hey, guys, I have a question: how is the solution given by  lpzCoville different from mine?

I'm sorry, I do not want to sound obnoxious, but I really am confused.

Thanks,
Ileana.
0

LVL 27

Expert Comment

ID: 10934620
Ileana
You have a good point!
As to my remark, notice that I wrote SECOND post, which is the one using arrays.

Dabas
0

LVL 4

Expert Comment

ID: 10934869
yup. didn't see your post, Ileana.
0

Expert Comment

ID: 11017613
Private Type infoDouble
strEFX as String
strTUC as String
strXPN As String
End Type

Private Sub SetMaxValueIndex(Byval ValueArray as Variant,Byref CurrentIndex as Long,ByRef MaxValueIndex as Long)
If Ubound(ValueArray) > CurrentIndex Then
If ValueArray(CurrentIndex) > ValueArray(MaxValueIndex) Then
MaxValueIndex = CurrentIndex
End If

SetMaxValueIndex ValueArray,CurrentIndex + 1,MaxValueIndex
End If
End Sub

Private Sub FindLargestValue
dim lngIndex as Long,lngMaxValueIndex as Long
Dim dblValueArray() as Double
Dim udtValueInfoArray() as infoDouble

Redim dblValueArray(5) as Double

dblValueArray(1) = ...
dblValueArray(2) = ...
....

Redim udtValueInfoArray(5) as infoDouble

udtValueInfoArray(1).strEFX = "..."
udtValueInfoArray(1).strTUC = "..."
udtValueInfoArray(1).strXPN = "..."
...

lngIndex = 1
lngMaxValueIndex = 1

SetMaxValueIndex udtValueArray,lngIndex,lngMaxValueIndex

With udtValueInfoArray(lngMaxValueIndex)
MsgBox "This is the highest value (" & udtValueArray(lngMaxValueIndex) & ")'s " _
& " info -> " & .strEFX & " " & .strTUC & " " & strXPN
End With
End Sub
0

LVL 2

Expert Comment

ID: 11037834
Thanks, Netminder!

Ileana.
0

Author Comment

ID: 11041289
My Appologies ileanarc!!  I definitely meant to give you the points.
0

LVL 2

Expert Comment

ID: 11041384
:)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is â€“ how do I become a web developer?
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable â€¦
Introduction to Processes
###### Suggested Courses
Course of the Month15 days, left to enroll