Results 1 to 8 of 8

Thread: Would someone please check my function? [SOLVED]

  1. #1

    Thread Starter
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692

    Would someone please check my function? [SOLVED]

    VB Code:
    1. Public Function ComparePlayerAnswer()
    2.     Dim SelectedAnswer, Msg As String
    3.     Dim IsCorrect As Boolean
    4.     Dim NumTries As Integer
    5.     NumTries = NumTries + 1
    6.     If IsCorrect = True Then
    7.         IsCorrect = False
    8.     End If
    9.     For i = 0 To frmQuestions.cmdChoice.Count - 1
    10.         If frmQuestions.cmdChoice(i).Value = True Then
    11.             SelectedAnswer = frmQuestions.cmdChoice(i).Caption
    12.         End If
    13.     Next i
    14.     If SelectedAnswer = QuestionArray(CurrentQuestion).Answer Then
    15.         Msg = "You are Correct."
    16.         IsCorrect = True
    17.         If NumTries = 1 Then
    18.             Msg = Msg & "  5 picks on the game board have now been granted to you."
    19.         Else
    20.             Msg = Msg & "  1 pick on the game board has now been granted to you."
    21.         End If
    22.     Else
    23.         MsgBox "Incorrect"
    24.     End If
    25.     If IsCorrect = True Then
    26.         MsgBox Msg
    27.     End If
    28. End Function

    For some reason, NumTries is resetting itself every time the program processes the function. As a result, it is displaying that it will give the player 5 picks on the game board when it should in fact only be giving 1.
    Last edited by hothead; Oct 5th, 2003 at 08:57 AM.

  2. #2
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Try making NumTries a global variable (or possible a static variable in that function).
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  3. #3

    Thread Starter
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692
    I never thought about that. Static will keep the value no matter what.

    But won't it keep the value forever? I guess I could use a static variable to accomodate how many times the function has been run, then reset NumTries when it changes. Or go like this:

    VB Code:
    1. If NumTries = NumTries + 3 Then NumTries = 1

    I shall try this. Thanks for the help.

  4. #4

    Thread Starter
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692
    Thanks, your help worked out rather well.

  5. #5
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Uhm...

    If NumTries = NumTries + 3 Then NumTries = 1

    Will always be false.

    Just as x=x+3, 0=3, 1=4, etc.

  6. #6

    Thread Starter
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692
    Not if the variable is declared as Static. It will always keep its previous value. If the answer is wrong, it redoes the entire function from the beginning, so after a few times of redoing the function, keeping its value from the previous attempt, it will be true.

  7. #7
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Originally posted by hothead
    Not if the variable is declared as Static. It will always keep its previous value. If the answer is wrong, it redoes the entire function from the beginning, so after a few times of redoing the function, keeping its value from the previous attempt, it will be true.
    Uhm...

    Regardless...

    Whatever NumTries equals, NumTries will never equal NumTries + 3.

    Just as 0 doesn't equal 3.

    3 <> 6, 7 <> 10, etc.

  8. #8

    Thread Starter
    Fanatic Member hothead's Avatar
    Join Date
    Mar 2002
    Location
    Missouri
    Posts
    692
    If you'll notice, I have a NumTries = NumTries + 1 at the beginning of the function.

    A static variable will keep its previous setting. So if I set NumTries to 1 by default, it will increment this by 1 the next time around.

    So....

    Second time it'll be 2.

    Third time it'll be 3.

    See how it works?
    Last edited by hothead; Oct 5th, 2003 at 06:21 PM.

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