5 Replies - 352 Views - Last Post: 14 October 2012 - 04:35 PM Rate Topic: -----

#1 LordAizen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 11-October 12

Accessing modules

Posted 14 October 2012 - 03:28 PM

I am doing a small project question game, to learn more about VB for college. I'm trying to access the questions that for no reason I have stored in the module, as you will see it's a public sub in the module with an array Question(0 to 9) as well as Answer(0 t 9). as you can see I'm generating a random number to see which question will be asked then I want to display it in a label, but I need to access the data from the module.
Module Questions
    Dim question(0 To 9) As String
    Dim Answer(0 To 9) As String

    Public Sub Questions()
        Question(0) = ""
        Answer(0) = ""
        Question(1) = ""
        Answer(1) = ""
        Question(2) = ""
        Answer(2) = ""
        Question(3) = ""
        Answer(3) = ""
        Question(4) = ""
        Answer(4) = ""
        Question(5) = ""
        Answer(5) = ""
        Question(6) = ""
        Answer(6) = ""
        Question(7) = ""
        Answer(7) = ""
        Question(8) = ""
        Answer(8) = ""
        Question(9) = ""
        Answer(9) = ""

    End Sub
End Module


I have omitted the questions and answers.
Public Class Game
    Dim Question_Number As Integer
    Dim rng As New System.Random


    Private Sub Game_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Question_Number = rng.Next(1, 11)
        Lbl_Queation.Text = Question_Number & " " & Question(0 To 9)

    End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Accessing modules

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Accessing modules

Posted 14 October 2012 - 03:51 PM

Shouldn't that be;
Lbl_Question.Text = Question_Number & " " & Question(Question_Number)

?
Where question number is the random integer for the index in the array?

There's quite a few issues with this code.
One, you have not declared the modules variables public
Two, you named a routine the same name as the module, so you are going to have to qualify it with the full name.

You really don't need a Questions sub you can just define the public variables and then fill them with the Questions and Answers for access in your game code. Otherwise, you would have to add Questions.Questions() in order to fill the array.

I'm not quite sure what 0 to 9 does in initializing the array as I don't use them very much but you should just be able to use Public Answers(9) as String, etc...

This post has been edited by CharlieMay: 14 October 2012 - 04:02 PM

Was This Post Helpful? 1
  • +
  • -

#3 LordAizen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 11-October 12

Re: Accessing modules

Posted 14 October 2012 - 04:00 PM

That probably an easier way yes, my logic was that I would generate a number then call the array and the number generated would be the one called which would then be put into the label which looking at it does seems flawed. Whereas yours means I generate a number call whichever number from the array and insert it into the label.
Though still unsure on how to call the array.

While I think it was you Charliemay who gave me the topic on the different scopes. even though the module should be accessible to all forms I'm unsure on how exactly to use the data inside it
Was This Post Helpful? 0
  • +
  • -

#4 LordAizen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 11-October 12

Re: Accessing modules

Posted 14 October 2012 - 04:09 PM

Thank you, I transferred the questions and answers to the bottom of the game_load sub and now when it loads it displays the question number but not the question not sure why yet. I'm going to try to figure it out, if you know why please give me some hints. Thanks again you've been very helpful Charlie.
Was This Post Helpful? 0
  • +
  • -

#5 LordAizen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 11-October 12

Re: Accessing modules

Posted 14 October 2012 - 04:18 PM

I've got it, after inserting a next question button below the array which was working I realised that it was because the initial code was above them so I just moved it now it works.
Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Accessing modules

Posted 14 October 2012 - 04:35 PM

Here, you might enjoy this
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1