Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

vb calculation

  • 30-01-2003 1:13am
    #1
    Closed Accounts Posts: 1,152 ✭✭✭


    hey everyone . im having a small bit of difficulty with vb not calculating a problem correctly...

    i have written the following code to calculate the amount of money a user recieves as Tax Free Allowance according to the number of kids one has..


    'calculates number of dependants if applicable
    Dim intChildren As Integer
    intChildren = Val(txtDependants.Text)
    If intChildren = 0 Then
    mcurDependants = 0
    ElseIf intChildren = 1 Or 2 Then
    mcurDependants = 300
    ElseIf intChildren = 3 Then
    mcurDependants = 400
    ElseIf intChildren = 4 Then
    mcurDependants = 500
    ElseIf intChildren > 4 Then
    mcurDependants = (intChildren * 100)

    End If


    now this looks correct (well to me anyway) but vb only calculates a result of 300 euro no matter what the number entered into txtDependants...

    thanks


Comments

  • Registered Users, Registered Users 2 Posts: 2,660 ✭✭✭Baz_


    Originally posted by sound_wave

    'calculates number of dependants if applicable
    Dim intChildren As Integer
    intChildren = Val(txtDependants.Text)
    If intChildren = 0 Then
        mcurDependants = 0
    [b]ElseIf intChildren = 1 Or 2 Then[/b]
        mcurDependants = 300
    ElseIf intChildren = 3 Then
        mcurDependants = 400
    ElseIf intChildren = 4 Then
        mcurDependants = 500
    ElseIf intChildren > 4 Then
        mcurDependants = (intChildren * 100)
        
    End If
    

    With if statements its important to understand how they are evaluated to true or false. In the first clause you're checking for a single value, which is okay, but in the second one youre checking a range (1-2) and you are seperating the two checks with the OR keyword. When you use OR it checks the first test "intChildren = 1", and if thats true it will execute the code inside that statement, if and only if the first statement is false it will check the second test, which in this case is "2" and if the test evaluates to non-zero, which 2 obviously does it is evaluated as true.

    The important point is that if you want to check if intChildren equals 1 or 2 you have to put
    [b]ElseIf intChildren = 1 Or intChildren = 2 Then[/b]
    

    in other words you cant shorten the second test (which seems logical, but isnt).

    hth

    Baz_


  • Registered Users, Registered Users 2 Posts: 6,240 ✭✭✭hussey


    It the line
    [PHP]ElseIf intChildren = 1 Or 2 Then[/PHP]
    as this will evalute to true always as you are asking

    if (inC = 1) or (2) .. and since 2 is always true
    it will always come in

    try
    [PHP]ElseIf intChildren = 1 Or intChildren = 2 Then[/PHP]


  • Closed Accounts Posts: 1,152 ✭✭✭sound_wave


    thanks everyone who posted a reply!!much appreciated!
    now to be finally finished with this damn program!:D

    the selet case statements do indeed appear to be more logically simple than the if statement..ill give that a go too

    thanks again to everyone who replied


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Originally posted by RicardoSmith
    Select case intChildren
    case = 0
    mcurDependants = 0
    case = 1
    mcurDependants = 300
    case = 2
    mcurDependants = 300
    case = 3
    mcurDependants = 400
    case = 4
    mcurDependants = 500
    case else
    mcurDependants = (intChildren * 100)
    end select

    Rather that
    case = 0
    
    it should be
    case 0
    
    .
    Note that in VB you can replace the two separate cases for 1 and 2 with either
    Case 1, 2
    
    (if intChildren is 1 or 2) or
    Case 1 To 2
    
    (if intChildren is between 1 and 2 inclusive, essentially the same thing in this case).
    We can also have
    Case Is > 4
    
    and then leave the default for error-trapping.

    I'm not a big fan of worrying over-much about efficiency, but Longs are more efficient than Integers in VB6 and earlier on 32-bit processors (Integers being 2byte in VB6 is a historical artefact, you should consider Longs to be the "real" Integers most of the time), and since this decreases rather than increases the chance of overflow there is no bad side in doing this. Thus I would have:
    Dim lChildren as Long
    lChildren = CLng(txtDependants.Text)
    Select Case lChildren
        Case 0
            mcurDependants = 0
        Case 1, 2
            mcurDependants = 300
        Case 3
            mcurDependants = 400
        Case 4
            mcurDependants = 500
        Case Is > 4
            mcurDependants = (lChildren * 100)
        Case Else
            Err.Raise 5 ' Standard VB error code for invalid argument.
    End Select
    

    The code calling this should be prepared to handle both error code 5 (meaning a negative number was entered), error code 13 (meaning a string that couldn't be converted to a number was entered) and error code 6(overflow) unless txtDependants.MaxLength ensures that overflow can't happen.

    Probably this can be done by just catching any error and prompting for a different value.


Advertisement