|
-
Mar 21st, 2013, 07:50 AM
#1
Thread Starter
Junior Member
[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.
-
Mar 21st, 2013, 08:12 AM
#2
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
-
Mar 21st, 2013, 08:17 AM
#3
Thread Starter
Junior Member
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!
-
Mar 21st, 2013, 11:21 AM
#4
Thread Starter
Junior Member
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!
-
Mar 21st, 2013, 11:24 AM
#5
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
-
Mar 21st, 2013, 11:27 AM
#6
Thread Starter
Junior Member
Re: Global Arrays
Should the module be a 'Public Sub' or 'Private Sub' ?
-
Mar 21st, 2013, 11:27 AM
#7
Re: Global Arrays
KL
I see that you snuck in a post while I was writing mine.
Did my comments help?
Spoo
-
Mar 21st, 2013, 11:29 AM
#8
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
-
Mar 21st, 2013, 11:39 AM
#9
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
-
Mar 21st, 2013, 11:39 AM
#10
Thread Starter
Junior Member
Re: Global Arrays
Amazing!!! So Happy! Thank You
-
Mar 21st, 2013, 11:46 AM
#11
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)
-
Mar 21st, 2013, 11:49 AM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|