Results 1 to 3 of 3

Thread: function question part 2

  1. #1

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

    Question function question part 2

    ok my function checks an integer from 0 to 100 ...

    depending on the number a select case statement will say what letter (a+, a, a-, b+ ...) is the equivalent to that integer n show it in the label (lblmpaverage) ... it works fine for every number except that when the average is 100 it only put 100 in the label n no letter next to it like it does for every other number ...

    Select Case cmbnumbers.ListIndex
    Case Is = 0
    average = ((a(1) + a(2) + a(3) + a(4) + a(5) + a(6) + a(7) + a(8) + a(9) + a(10) + a(11) + a(12) + a(13) + a(14) + a(15) + a(16) + a(17) + a(18) + a(19) + a(20) + a(21) + a(22) + a(23) + a(24) + a(25)) / 25)
    Case Is = 1
    average = ((a(1) + a(2) + a(3) + a(4) + a(5) + a(6) + a(7) + a(8) + a(9) + a(10) + a(11) + a(12) + a(13) + a(14) + a(15) + a(16) + a(17) + a(18) + a(19) + a(20) + a(21) + a(22) + a(23) + a(24)) / 24)
    Case Is = 2
    average = ((a(1) + a(2) + a(3) + a(4) + a(5) + a(6) + a(7) + a(8) + a(9) + a(10) + a(11) + a(12) + a(13) + a(14) + a(15) + a(16) + a(17) + a(18) + a(19) + a(20) + a(21) + a(22) + a(23)) / 23)
    Case Is = 3
    average = ((a(1) + a(2) + a(3) + a(4) + a(5) + a(6) + a(7) + a(8) + a(9) + a(10) + a(11) + a(12) + a(13) + a(14) + a(15) + a(16) + a(17) + a(18) + a(19) + a(20) + a(21) + a(22)) / 22)
    Case Is = 4
    average = ((a(1) + a(2) + a(3) + a(4) + a(5) + a(6) + a(7) + a(8) + a(9) + a(10) + a(11) + a(12) + a(13) + a(14) + a(15) + a(16) + a(17) + a(18) + a(19) + a(20) + a(21)) / 21)
    Case Is = 5
    average = ((a(1) + a(2) + a(3) + a(4) + a(5) + a(6) + a(7) + a(8) + a(9) + a(10) + a(11) + a(12) + a(13) + a(14) + a(15) + a(16) + a(17) + a(18) + a(19) + a(20)) / 20)
    Case Is = 6
    average = ((a(1) + a(2) + a(3) + a(4) + a(5) + a(6) + a(7) + a(8) + a(9) + a(10) + a(11) + a(12) + a(13) + a(14) + a(15) + a(16) + a(17) + a(18) + a(19)) / 19)
    Case Is = 7

    Call test(average)
    letter = test(average)

    lblmpaverage.Caption = average & " [" & letter & "]"

    this is my function ...

    Public Function test(x As Integer)

    Dim letter As String

    Select Case x
    Case Is >= 97
    letter = "A+"
    Case Is >= 93
    letter = "A"
    Case Is >= 90
    letter = "A-"
    Case Is >= 87
    letter = "B+"
    Case Is >= 83
    letter = "B"
    Case Is >= 80
    letter = "B-"
    Case Is >= 77
    letter = "C+"
    Case Is >= 73
    letter = "C"
    Case Is >= 70
    letter = "C-"
    Case Is >= 65
    letter = "D"
    Case Is >= 60
    letter = "F"
    Case Is >= 0
    letter = "F-"
    End Select

    test = letter

    End Function

    y won't it give a letter to the number 100 ????

  2. #2
    Guest

    Question

    It worked for me, but I have made a small modification to your code. For your Function, I added a ByVal keyword.
    Code:
    Public Function test(ByVal x As Integer)
    Next, I put the following code in a CommandButton.
    Code:
    Private Sub Command1_Click()
    
        Dim MyAverage As Integer
        Dim Letter As String
        
        ' Set the Average to 100
        MyAverage = 100
        Letter = test(MyAverage)
    
        'Print the result
        Print Letter
    
    End Sub

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    A couple of comments:

    1) You can eliminate your Select Case and just do this

    average = ((a(1) + a(2) + a(3) + a(4) + a(5) + a(6) + a(7) + a(8) + a(9) + a(10) + a(11) + a(12) + a(13) + a(14) + a(15) + a(16) + a(17) + a(18) + a(19) + a(20) + a(21) + a(22) + a(23) + a(24) + a(25)) / (25 - cmbnumbers.ListIndex)

    2) You can remove Call test(average) since you do it again with letter = test(average). As a matter of fact you can just do the following if you want and eliminate the second call to test as well.

    lblmpaverage.Caption = average & " [" & test(average) & "]"


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