CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: if and elseif

  1. #1
    Join Date
    May 2001
    Location
    MO, USA
    Posts
    87

    if and elseif

    okay here's my code,

    Sub SubComputeArea_Click()
    Dim sum As Long
    Dim sum1 As Long
    Dim sum2 As Long
    sum1 = CLng(Text1)
    sum2 = CLng(Text2)
    sum = (sum1 + sum2)
    Text3 = sum
    If Text3 <= 61 Then
    Text4 = 107.16 * 0.4699
    Text5 = 115.64 * 0.4699
    Text6 = 107.16 * 0.62
    Text7 = 115.64 * 0.62
    End If

    If Text3 >= 61 Then
    Text4 = 124.72 * 0.4699
    Text5 = 134.6 * 0.4699
    Text6 = 124.72 * 0.62
    Text7 = 134.6 * 0.62
    End If
    End Sub

    okay the prob is i have about 7 more >=#'s to put in, how can this be done without making a total mess of this code...please help

    thanx in advance

    midnightservice


  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: if and elseif

    Well, you could use If Then ElseIf as you suggested, or you could use a SLECT CASE

    If Text3 <= 61 then
    Text4 = 107.16 * 0.4699
    Text5 = 115.64 * 0.4699
    Text6 = 107.16 * 0.62
    Text7 = 115.64 * 0.62
    ElseIf Text3 >= 61 then
    Text4 = 124.72 * 0.4699
    Text5 = 134.6 * 0.4699
    Text6 = 124.72 * 0.62
    Text7 = 134.6 * 0.62
    End If

    Select Case Text3
    Case is <= 61
    Text4 = 107.16 * 0.4699
    Text5 = 115.64 * 0.4699
    Text6 = 107.16 * 0.62
    Text7 = 115.64 * 0.62
    Case is >= 61
    Text4 = 124.72 * 0.4699
    Text5 = 134.6 * 0.4699
    Text6 = 124.72 * 0.62
    Text7 = 134.6 * 0.62
    End Select




    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  3. #3
    Join Date
    Sep 2001
    Location
    Little Rock, Arkansas
    Posts
    40

    Re: if and elseif

    Depending on what the other 7 IFs look like, a variation might be something like:

    Select Case Text3
    Case is <= 61
    Value1 = 107.16
    Value2 = 115.64
    Case is >= 61
    Value1 = 124.72
    Value2 = 134.6
    End Select

    Text4 = Value1 * 0.4699
    Text5 = Value2 * 0.4699
    Text6 = Value1 * 0.62
    Text7 = Value2 * 0.62





  4. #4
    Join Date
    May 2001
    Location
    MO, USA
    Posts
    87

    Re: if and elseif

    okay works up to a point....here is what i got

    Sub SubComputeArea_Click()
    Dim sum As Long
    Dim sum1 As Long
    Dim sum2 As Long
    sum1 = CLng(Text1)
    sum2 = CLng(Text2)
    sum = (sum1 + sum2)
    Text3 = sum
    Select Case Text3
    Case 0 To 61
    Value1 = 107.16
    Value2 = 115.64
    Case 62 To 72
    Value1 = 124.72
    Value2 = 134.6
    Case 73 To 81
    Value1 = 142.29
    Value2 = 153.6
    Case 82 To 91
    Value1 = 159.76
    Value2 = 172.52
    Case 92 To 101
    Value1 = 177.42
    Value2 = 191.48
    Case 102 To 111
    Value1 = 194.99
    Value2 = 210.43
    Case 112 To 121
    Value1 = 212.56
    Value2 = 229.39
    Case 122 To 131
    Value1 = 230.12
    Value2 = 248.35
    End Select
    Text4 = Value1 * 0.4699
    Text5 = Value2 * 0.4699
    Text6 = Value1 * 0.62
    Text7 = Value2 * 0.62
    End Sub


    what is wrong...evrything from case 91 and up returns either a 0 value or a value from the begining of the case....any ideas

    midnightservce


  5. #5
    Join Date
    May 2001
    Location
    MO, USA
    Posts
    87

    Re: if and elseif

    okay i got it to work but any case number 100 returns the same value as case 0 to 61....why is this.....

    midnightserive


  6. #6
    Join Date
    Sep 2001
    Location
    Little Rock, Arkansas
    Posts
    40

    Re: if and elseif

    I'm not at a machine where I can test your code, but if you are getting incorrect output you should be able to test it yourself. You could put a breakpoint at the beginning of the Case statement and step through your code a line at a time and see where it was going wrong.

    Even if I "know" a particular condition will "never" happen, I always put a "Case Else" clause at the end of my case statement with a MsgBox saying that the values were outside the range of the statement (less than 0, greater than 131).


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured