Results 1 to 5 of 5

Thread: RESOLVED Where can I move this code?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2002
    Location
    Auckland
    Posts
    17

    RESOLVED Where can I move this code?

    I am not very good at this and don't quite know how to word my question.

    I have a program where the user selects the number of questions they want to be asked and then clicks the start button. When the start button is pressed the program checks how many questions the user is going to be asked and sets the variable. After this the start button is disabled and the user uses the next button for the next question.


    What can I do with the question generation code so it is not contained within the start and next buttons click code?

    As you can see the program just asks random multiplication questions.

    Randomize
    intResult1 = Int((12 * Rnd) + 1)
    intResult2 = Int((12 * Rnd) + 1)
    QuestionText.Caption = intResult1 & "X" & intResult2 & " ="

    Thanks.
    Last edited by Crazy Spas; Jun 22nd, 2006 at 05:53 PM.

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Where can I move this code?

    Create a Sub and call it as required:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Randomize
    5. End Sub
    6.  
    7. Private Sub xx()
    8.     Call Generate_Question
    9. End Sub
    10.  
    11. Private Sub Generate_Question()
    12.     QuestionText.Caption = CStr(Int((12 * Rnd) + 1) & "X" & Int((12 * Rnd) + 1) & " =")
    13. End Sub

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2002
    Location
    Auckland
    Posts
    17

    Re: Where can I move this code?

    Thank you very much. That works perfectly.

  4. #4
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: RESOLVED Where can I move this code?

    No probs.

    A couple of points:
    1. The Randomize only needs to be called once per session, hence I placed it in Form_Load(),
    2. I did away with those intermediate variables, and
    3. Cast the result as a String Data Type (CStr()) for the .Caption (not iperative to change from and Int to String, but good practice).

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2002
    Location
    Auckland
    Posts
    17

    Re: RESOLVED Where can I move this code?

    Thank you. It looks much nicer.

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