Results 1 to 12 of 12

Thread: [RESOLVED] Global Arrays

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    16

    Resolved [RESOLVED] Global Arrays

    Hi,
    I'm doing my computing soursework for school and have come across one final problem which I though would be very simple to correct.
    All I need to do is define 2 different arrays so that they are all global and accessable by all forms.
    I have tried declairing them as a global variable ;

    Dim Player(0 to 2) as string
    Dim Score(0 to 2) as integer

    This does not work as the array is only accessable to one form.
    I have also tried;

    Public Player(0 to 2) as string
    Public Score(0 to 2) as integer

    This returns an error saying arrays cannot be global.

    Any help would be amazing! Thanks in advance.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Global Arrays

    Welcome to the forums

    If you put
    Code:
    Public Player(0 to 2) as string
    Public Score(0 to 2) as integer
    in a module it will work just fine. Then on a form you could do
    Code:
    Private Sub Command1_Click()
    'you can do something like
    Dim UseArray As String
    UseArray = Player(0)
    End Sub

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    16

    Re: Global Arrays

    Thanks very much for the speedy reply!
    I'll give that a go tonight. I think i might need a little bit of further help but I'll see how it goes!
    Thank You!

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    16

    Re: Global Arrays

    No, I still cant get it to work :/

    The problem now is that when The user is asked to input the players names ( player(0 to 2) ) it crashes because it cant find the deceleration of 'Player'.

    I would upload it but my readability is awful!

  5. #5
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Global Arrays

    KL

    Hack has given you everything you should need.

    In the event that "adding a module" is new to you, the approach is similar to that of adding a form ...
    > Click the Project menu option
    > Click the Add Module option
    > Then you can paste in Hack's declarations from his first code frag.

    Hope that helps.
    Spoo

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    16

    Re: Global Arrays

    Should the module be a 'Public Sub' or 'Private Sub' ?

  7. #7
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Global Arrays

    KL

    I see that you snuck in a post while I was writing mine.
    Did my comments help?

    Spoo

  8. #8
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Global Arrays

    KL

    The module will just have a name
    The code that Hack provided are referred to as "declarations"
    They go at the top of the code box, and do not need any designation such as Public Sub or Private Sub

    Spoo

  9. #9
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Global Arrays

    KL

    Just to enhance a bit...

    A module can contain both Public Subs as well as Private Subs.
    It can even contain Public Functions.

    Nonetheless, "declarations" must go at the very top, above what would
    be the first Sub or Function.

    Hope that helps.
    Spoo

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    16

    Re: Global Arrays

    Amazing!!! So Happy! Thank You

  11. #11
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: [RESOLVED] Global Arrays

    klivingston, you might find the article Understanding the Scope of Variables very helpful as well.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  12. #12
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: [RESOLVED] Global Arrays

    KL

    Hooray !!

    Glad you got it to work.
    And also nice to see that you marked the thread as Resolved.

    We're here to serve ,,
    Spoo

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