Link to home
Start Free TrialLog in
Avatar of DapperDan
DapperDan

asked on

Share an array between forms

Hello Experts. I think this is an easy one.

I tried to declare an array as public in my main form, as it must be accessed by various other forms. However, arrays cannot be public apparently.

What is the simplest way of making it available to other modules/forms?  

Thanks
ASKER CERTIFIED SOLUTION
Avatar of bobbit31
bobbit31
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of beckingh
beckingh

Add a module Globals.bas

and declare your array as Global there.
Add New Module to your project, ex: modMain.bas

Declare the following:
Public garyParameterArray() as String

The above declaration is dynamic array, after declaring, you can use it anywhere in your project, just do the following:

    ReDim garyParameterArray(0 To 0)
    garyParameterArray(0) = "TEST"

So that u can redim anywhere u want with whatever size u want and use it.

Chandu
Avatar of Richie_Simonetti
bobbit31 has the answer.
If you like to work on other way, you must reference the array with the name of the module/form that contains it:

form1.myarray
or whatever you named both.
You could also, try declaring a string as public, in your module, or the main form. And use Split and Join functions of VB6, provided, your array is not that big.

Public gStrArray As String

In your passing form,

gStrArray = Join(yourArray,",")

In your getting form,

Dim localArray
localArray = Split(gStrArray,",")

Hope this helps,
Cheers.

If you declare it in your main form, then the public variable should be prefixed with the form name and a dot, like, mainform.gStrArray.
Avatar of DapperDan

ASKER

Sorry for the delay in checking back. Thanks for all your comments. I will try this out when I get home from work tonight (UK time).

Cheers
That was annoyingly simple. I have a lot to learn about VB :).

Seems fairest to give points to the first reply, but thanks to everyone.