If statement in select case problem 
Author Message
 If statement in select case problem

I'm having problems with the code below and would appreciate some help
finding the problem. Explanation of the code follows.

The difference in years between two dates is stored in yrsInPlace.
In reqval I determine what the value of salTI1 will be if it increases
by the percentage value in yrsInPlace p/a for the number of years in
yrsInPlace.
This value will be assigned to reqvalue.

The if statement that follows does a number of comparisons and assigns
a value of 0, 1 or 2 to expr. Then a select case statement runs some
code depending on the value of expr.

The problem I'm having is that the if then statememts in the select
case expression is not returning the right values.
eg. If expr = 1 and testi1 = 200 and testf2 = 100 the if statement in
case 1 will assign a value of 0 to p4 instead of 2 (it should return 2
because 200 > 100).

Is there a problem with this code, or is my problem somewhere else?

testi1 = session("testi1")
testf1 = session("testf1")
testi2 = session("testi2")
testf2 = session("testf2")
testi3 = session("testi3")
testf3 = session("testf3")
dateS1 = session("dateS1")
dateE1 = session("dateE1")
salTI1 = session("totali1")
totalf1 = session("totalf1")
totali2 = session("totali2")
totalf2 = session("totalf2")

yrsInPlace = (DateDiff("yyyy",dateS1,dateE1))
annualinc = 1.07
reqval = cint(salTI1 * annualinc ^ yrsInPlace)

if ( testi1 > 0 and testf1 > 0 ) and ( testi2 = 0 and testf2 = 0 ) and
( testi3 = 0 and testf3 = 0 ) then
        expr = 0
 elseif ( testi2 > 0 and testf2 > 0 ) and ( testi3 = 0 and testf3 = 0
) then
        expr = 1
 elseif ( testi2 > 0 and testf2 > 0 ) and ( testi3 > 0 and testf3 > 0
) then
        expr = 2
end if

select case expr
 case 0
        if totalf1 > reqval then
                p4 = 2
        else
                p4 = 0
        end if
 case 1
        if testi1 > testf2 then
                p4 = 2
        else
                p4 = 0
        end if
 case 2
        if ((salTI1 > totalf2) and (totali2 > testf3)) then
                p4 = 2
        else
                p4 = 0
        end if
end select



Tue, 08 Mar 2005 18:31:31 GMT  
 If statement in select case problem
Without trying to follow the logic, I'll make one observation...

Your if/elseif/elseif/end if logic for assigning a value to expr is all conditional (there's no simple else default fall-through).  If none of the if conditions are true then no default value is explicitly assigned to expr.  If the select case refers to a variable that has *not* been assigned an explicit value (i.e., IsEmpty(expr) would evaluate to true) then it will have an implicit (coerced) value of 0 when compared to any numeric literal.

Here's what I suggest you do...

Emulate this logic in a WSH hosted wscript and step through it in a script de{*filter*}.

Here's how you can structure the test script and emulate the Session object.

'===================================================
' create a dictionary to play the part of the
' Session collection and load it with test values.
'
Set session = CreateObject("Scripting.Dictionary")

session("testi1")  = testvalue
session("testf1")  = testvalue
session("testi2")  = testvalue
session("testf2")  = testvalue
session("testi3")  = testvalue
session("testf3")  = testvalue
session("dateS1")  = testvalue
session("dateE1")  = testvalue
session("totali1") = testvalue
session("totalf1") = testvalue
session("totali2") = testvalue
session("totalf2") = testvalue

'===================================================
' this is the actual code from your page that you
' posted.  
' I reformatted it a bit for better readability...
'
testi1  = session("testi1")
testf1  = session("testf1")
testi2  = session("testi2")
testf2  = session("testf2")
testi3  = session("testi3")
testf3  = session("testf3")
dateS1  = session("dateS1")
dateE1  = session("dateE1")
salTI1  = session("totali1")
totalf1 = session("totalf1")
totali2 = session("totali2")
totalf2 = session("totalf2")

yrsInPlace = (DateDiff("yyyy",dateS1,dateE1))
annualinc = 1.07
reqval = cint(salTI1 * annualinc ^ yrsInPlace)

if ( testi1 > 0 and testf1 > 0 ) _
and ( testi2 = 0 and testf2 = 0 ) _
and ( testi3 = 0 and testf3 = 0 ) then
  expr = 0
elseif ( testi2 > 0 and testf2 > 0 ) _
and ( testi3 = 0 and testf3 = 0 ) then
  expr = 1
elseif ( testi2 > 0 and testf2 > 0 ) _
and ( testi3 > 0 and testf3 > 0 ) then
  expr = 2
end if

'===================================================
' sanity check...

if IsEmpty(expr) then
    msgbox "expr is empty"
end if

select case expr
  case 0
    if totalf1 > reqval then
      p4 = 2
    else
      p4 = 0
    end if
  case 1
    if testi1 > testf2 then
      p4 = 2
    else
      p4 = 0
    end if
 case 2
    if ((salTI1 > totalf2) and (totali2 > testf3)) then
      p4 = 2
    else
      p4 = 0
    end if
end select

--
Michael Harris
Microsoft.MVP.Scripting
Seattle WA US



Wed, 09 Mar 2005 11:57:00 GMT  
 If statement in select case problem
Thanks Michael

I'm afraid you lost me on the "Emulate this logic in a WSH hosted
wscript" part of "Emulate this logic in a WSH hosted wscript and step
through it in a script de{*filter*}."

Quote:
> Your if/elseif/elseif/end if logic for assigning a value to expr is all
> conditional (there's no simple else default fall-through).

I realised that after posting.

Is the rest of the code that I posted correct - even if it's not the
most efficient (as you stated, "there's no simple else default
fall-through")? i.e. Are there no blatant errors that I've missed?

Thanks again...



Sun, 13 Mar 2005 14:38:35 GMT  
 If statement in select case problem

Quote:
> Without trying to follow the logic, I'll make one observation...

Which I've fixed! Thanks for pointing it out.

Quote:
> Here's what I suggest you do...

Here's what I DID do, and it seems to have worked.

Quote:
> testi1  = session("testi1")
> testf1  = session("testf1")
> testi2  = session("testi2")
> testf2  = session("testf2")
> testi3  = session("testi3")
> testf3  = session("testf3")
> dateS1  = session("dateS1")
> dateE1  = session("dateE1")
> salTI1  = session("totali1")
> totalf1 = session("totalf1")
> totali2 = session("totali2")
> totalf2 = session("totalf2")

I had another look at all the code with some fresh Monday morning eyes
and converted all the values that are held in the sessions above to
type csng. The result is:

testi1  = csng(session("testi1"))
testf1  = csng(session("testf1"))
testi2  = csng(session("testi2"))
testf2  = csng(session("testf2"))
testi3  = csng(session("testi3"))
testf3  = csng(session("testf3"))
dateS1  = session("dateS1")
dateE1  = session("dateE1")
salTI1  = csng(session("totali1"))
totalf1 = csng(session("totalf1"))
totali2 = csng(session("totali2"))
totalf2 = csng(session("totalf2"))

Now when I enter data in each of the text fields on the screens and
run the code in the select case statement the value of p4 is 0 when it
is supposed to be 0 and 2 when it is supposed to be 2! Just what I
wanted.

Thanks again for all the help.



Sun, 13 Mar 2005 17:17:20 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Problem with Expression in a Select Case Statement

2. VBScript Select Case vs VB Select Case

3. Select Case statement in VBScript

4. Select Case Statement

5. Select Case Statement

6. Select Case Statement

7. Is the Select Case Statement crippled in VBScript?

8. Select Case Statements

9. Select Case Statement

10. SELECT CASE STATEMENT

11. Select Case Statement Help

12. Select Case Statement

 

 
Powered by phpBB® Forum Software