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

Is there an easier way to re-code this? 2

Status
Not open for further replies.

Trope

Programmer
May 14, 2001
110
US
It appears that I am ALMOST rewriting the same code - is there an easier way to code this than all these IF's?

'------CODE SNIPPET----------------
' Did they order just one job?
IF multiple_quantity = 1 THEN
' Determine Hire Type
IF multiple_hire_type = "D" THEN
' Direct Hires
priceEach = FormatCurrency(jobOpeningPrices.Fields.Item("direct1").Value)
ELSE
' Contract Hires
priceEach = FormatCurrency(jobOpeningPrices.Fields.Item("contract1").Value)
END IF
END IF

' Do they want between 2-10 job openings?
IF multiple_quantity >1 AND multiple_quantity<=10 THEN
' Determine Hire Type
IF multiple_hire_type = &quot;D&quot; THEN
' Direct Hires
priceEach = FormatCurrency(jobOpeningPrices.Fields.Item(&quot;direct2&quot;).Value)
ELSE
' Contract Hires
priceEach = FormatCurrency(jobOpeningPrices.Fields.Item(&quot;contract2&quot;).Value)
END IF
END IF

' Do they want between 11-25 job openings?
IF multiple_quantity >10 AND multiple_quantity<=25 THEN
' Determine Hire Type
IF multiple_hire_type = &quot;D&quot; THEN
' Direct Hires
priceEach = FormatCurrency(jobOpeningPrices.Fields.Item(&quot;direct3&quot;).Value)
ELSE
' Contract Hires
priceEach = FormatCurrency(jobOpeningPrices.Fields.Item(&quot;contract3&quot;).Value)
END IF
END IF

' Do they want between 11-25 job openings?
IF multiple_quantity >= 50
' Determine Hire Type
IF multiple_hire_type = &quot;D&quot; THEN
' Direct Hires
priceEach = FormatCurrency(jobOpeningPrices.Fields.Item(&quot;direct4&quot;).Value)
ELSE
' Contract Hires
priceEach = FormatCurrency(jobOpeningPrices.Fields.Item(&quot;contract4&quot;).Value)
END IF
END IF

' Calculate the Price and Total
total = multiple_quantity * priceEach
 
how about

dim sType

if multiple_hire_type = &quot;D&quot; then
sType = &quot;direct&quot;
else
sType = &quot;contract&quot;
end if

if multiple_quantity = 1 then
priceEach = FormatCurrency(jobOpeningPrices.Fields.Item(sType & &quot;1&quot;).Value)
elseif multiple_quantity > 1 and multiple_quantity <= 10 then
priceEach = FormatCurrency(jobOpeningPrices.Fields.Item(sType & &quot;2&quot;).Value)
elseif multiple_quantity > 10 and multiple_quantity <= 25 then
priceEach = FormatCurrency(jobOpeningPrices.Fields.Item(sType & &quot;3&quot;).Value)
elseif multiple_quantity >= 50 then
priceEach = FormatCurrency(jobOpeningPrices.Fields.Item(sType & &quot;4&quot;).Value)
end if

' Calculate the Price and Total
total = multiple_quantity * priceEach

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
And if you want to see even less code:
Code:
dim sType, sQty

If multiple_hire_type = &quot;D&quot; Then
    sType = &quot;direct&quot;
Else
    sType = &quot;contract&quot;
End If

If multiple_quantity = 1 Then
    sQty = 1
ElseIf multiple_quantity > 1 And multiple_quantity <= 10 Then
    sQty = 2
ElseIf multiple_quantity > 10 and multiple_quantity <= 25 Then
    sQty = 3
ElseIf multiple_quantity >= 50 Then
    sQty = 4
End If

priceEach = FormatCurrency(jobOpeningPrices.Fields.Item(sType & sQty).Value)

' Calculate the Price and Total
total = multiple_quantity * priceEach
 
Even prettier is
Code:
dim sType, sQty

If multiple_hire_type = &quot;D&quot; Then
    sType = &quot;direct&quot;
Else
    sType = &quot;contract&quot;
End If

If multiple_quantity = 1 Then
    sQty = 1
ElseIf multiple_quantity <= 10 Then
    sQty = 2
ElseIf multiple_quantity <= 25 Then
    sQty = 3
ElseIf multiple_quantity >= 50 Then
    sQty = 4
End If

priceEach = FormatCurrency(jobOpeningPrices.Fields.Item(sType & sQty).Value)

' Calculate the Price and Total
total = multiple_quantity * priceEach
Note, however, the flaw (I'm guessing) in the original logic: if multiple_quanity is 25 to 49 then nothing happens. Same with these rewrites.

And of course there's no error checking to verify that it's even a number we're dealing with, etc.
 
Shoot, there is an error.

I need 1 job opening to equal price1.

2-10 openings to equal price2.

11-25 equals a third price and finally,

26-50 a fourth price.

Do I need to add more code?
 
No, in my second example you can just change
Code:
ElseIf multiple_quantity >= 50 Then
to
Code:
ElseIf multiple_quantity <= 50 Then
for example. In all of the code it indicates &quot;greater than or equal to 50&quot; rather than &quot;less than or equal to 50,&quot; which makes all the difference.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top