Results 1 to 7 of 7

Thread: small example of my large program

  1. #1

    Thread Starter
    Hyperactive Member theman32x's Avatar
    Join Date
    May 2000
    Location
    New Jersey, USA
    Posts
    305

    Exclamation

    what this program is supposed to do is find the average of 2 grades ... then display the average n depending on what the average is ... it will give u a letter value ... (please try this on ur comp n see if it works for u or not)
    u need 4 textboxes, 1 command button

    Private Sub Command1_Click()
    Dim a1 As Integer, a2 As Integer, b As Integer, c As String
    a1 = Text1.Text
    a2 = Text2.Text
    b = (a1 + a2) / 2
    Text3.Text = b
    Call test(b)
    c = test(b)
    Text4.Text = c
    End Sub

    Public Function test(b As Integer)
    Dim wha As String
    Select Case b
    Case Is >= 97
    wha = "A+"
    Case Is >= 55
    what = "C"
    Case Is >= 0
    wha = "F"
    test = wha
    End Select
    End Function

    i don't know y it won't give me the value letters for anything above 54 ...

  2. #2

  3. #3
    Lively Member
    Join Date
    Jul 2000
    Posts
    82
    hi,
    i know what the problem is and will explain it before writing the correct code. the problem is in the select case. say you were given to say if 88 is larger than 55 or larger than 0, you would say that both are correct, right? that is exactly what is going on in the select case you wrote so, here is the code not in a select case (i dont like it, and it is not ussefull here):


    Private Sub Command1_Click()
    Dim a1, a2, b As Integer, c As String
    a1 = Text1.Text
    a2 = Text2.Text
    b = (a1 + a2) / 2
    Text3.Text = b
    Call test(b)
    c = test(b)
    Text4.Text = c
    End Sub

    Public Function test(b As Integer)
    Dim wha As String
    If b >= 97 Then wha = "A+"
    If b >= 55 And b < 97 Then wha = "C"
    If b >= 0 And b < 55 Then wha = "F"
    test = wha
    End Function

    hope u like it and it is helpfull, it worked on my computer, bye,
    yair

  4. #4

    Thread Starter
    Hyperactive Member theman32x's Avatar
    Join Date
    May 2000
    Location
    New Jersey, USA
    Posts
    305

    i don't think u understood my question

    i agree with ur example that 0 n 55 r less than 88 ... making an if then statement saying that it should be x >= n x <= .... but my select case code works fine ... any number it will give the right letter value to ... EXCEPT for anything bigger than 100 ... i don't know y ...

  5. #5
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    Select Case is the right thing to use here, ysa1441 has highlighted a C++ thing with the switch statement (the C++ equivilent of Select Case) which doesn't apply here.

    there may be others but you have a bug here

    Code:
    Public Function test(b As Integer) 
    Dim wha As String 
    Select Case b 
    Case Is >= 97 
    wha = "A+" 
    Case Is >= 55 
    what = "C" 
    Case Is >= 0 
    wha = "F" 
    
    test = wha   'Here Is Your bug.  
    End Select   'By putting test = wha before End Select it
                 'only sets the return value if the grade is an F
                 ' Switch these two statement's around
    
    
    
    End Function

  6. #6

    Thread Starter
    Hyperactive Member theman32x's Avatar
    Join Date
    May 2000
    Location
    New Jersey, USA
    Posts
    305

    fix that

    ok i put the test = wha after the select case ... i only get the letter value for anything from 97 n up (a+) n 55 n below (f) i don't get the letter value for anything ranging from 56 to 96

  7. #7
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    Code:
    Public Function test(b As Integer)
    Dim wha As String
    Select Case b
    Case Is >= 97
    wha = "A+"
    Case Is >= 55
    what = "C"  'Bug Here You put what instead of wha
    Case Is >= 0
    wha = "F"
    End Select
    test = wha
    End Function

    You really should use option explicit at the begining of your code, it sorts out bugs like that before they happen.

    go to options and check require variable declaration, that puts it in automaticly.

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