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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to avoid huge If/Case statements!!! 1

Status
Not open for further replies.

jojo11

Programmer
Feb 2, 2003
189
US
I have to create a fuction that accepts 6 parameters (all bit (0/1) fields and returns a string. I have to account for every permutation. There are obviously 2^6 = 64 possible permutations.
ex.
0 0 0 0 0 0
0 0 0 0 0 1
0 0 0 0 1 0
etc....

Is there a better way to do this than 64 seperate statements?

Thanks in advance.

-------------------------------------------
When you have spent all day trying to work through a problem, it is best to take a step back, reevaluate the problem, then work on the next project and never let the old one show its ugly face again!!
 
What are you trying to do with the function? Is there 64 possible directions the function could take?

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
I should have said that. Sorry.

The function takes 6 bit fields from a sql table. Depending on the value of these fields, one of three values will be the output. To be exact, it will be a insurance claim type.

-------------------------------------------
When you have spent all day trying to work through a problem, it is best to take a step back, reevaluate the problem, then work on the next project and never let the old one show its ugly face again!!
 
Maybe I am missing something but, should there not be only three conditions to output? Generally bit fields are set up so you can pass one value and pick the conditions off by "And"ing off the particular bits to get the conditions that are present. For example if a value of 14 is passed to a function you could perform something like this

if value and 1 then bit1 is on
if value and 2 then bit2 is on
if value and 4 then bit3 is on
if value and 8 then bit4 is on

and a value of 14 would output that bit2, bit3 and bit4 were on. Does that help or am I out in left field for what you are trying to do? [spin]

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Well if there's only 1 of 3 options for an output, wouldn't you only have a need for 3 conditions rather than 64?

--
Jonathan
 
Here is an ugly example of what I need to do.

Public Function ClaimType(bit1 as int, bit2 as int, ... bit6 as int) as string

If bit1 = 0 AND bit2 = 0 AND bit3 = 0 AND bit4 = 0 AND bit5 = 0 AND bit6 = 1 THen
ClaimType = "ClaimtypeA"
End If

If bit1 = 0 AND bit2 = 0 AND bit3 = 0 AND bit4 = 0 AND bit5 = 1 AND bit6 = 1 THen
ClaimType = "ClaimtypeB"
End If

If bit1 = 0 AND bit2 = 0 AND bit3 = 0 AND bit4 = 1 AND bit5 = 1 AND bit6 = 1 THen
ClaimType = "ClaimtypeA"
End If

If bit1 = 0 AND bit2 = 0 AND bit3 = 1 AND bit4 = 1 AND bit5 = 1 AND bit6 = 1 THen
ClaimType = "ClaimtypeC"
End If


End Function

-------------------------------------------
When you have spent all day trying to work through a problem, it is best to take a step back, reevaluate the problem, then work on the next project and never let the old one show its ugly face again!!
 
What about something like this

Public Function ClaimType(bit1 As Integer, bit2 As Integer, bit3 As Integer, bit4 As Integer, bit5 As Integer, bit6 As Integer) As String
Dim data As Integer

data = (bit1 * 1) + (bit2 * 2) + (bit3 * 4) + (bit4 * 8) + (bit5 * 16) + (bit6 * 32)
Select Case data
Case 32, 56
ClaimType = "ClaimtypeA"
Case 48
ClaimType = "ClaimtypeB"
Case 60
ClaimType = "ClaimtypeC"
End Select

End Function

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Foada's answer is a good one.

Also, to save yourself some time in the future, you don't need to do all those comparisons. You can do this and save yourself some typing:
Code:
  If Not bit1 AND not bit2 AND not bit3 AND not bit4 AND not bit5 AND bit6 Then
which can be simplified a little more with DeMorgan's Theorem:
Code:
  If Not (bit1 OR bit2 OR bit3 OR bit4 OR bit5) AND bit6 Then
Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top