Public Dynamic Arrays

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm trying to publically define a dynamic array, arrSub() in the declarations
section of my code for a particular form. The array is designed to accept a
chaning number of string variables, and will be used by other forms and
modules throughout the running of my program.

The following code begins my module:

Option Compare Database
Option Explicit
Option Base 1

Public arrSub() As Variant

When I debug, I get an error message: "Constants, fixed-length strings,
arrays, user-defined types and Declare statements not allowed as Public
members of object modules."

The Help file gives the following advice: "Although a procedure can't return
an array, it can return a Variant that contains an array. To simulate a
Public array in a class module, use a set of Property procedures that accept
and return a Variant containing an array."

I'm not sure what to make of that, but I know that dynamic arrays can be
defined as public somehow. Does anyone have any ideas. Many thanks in
advance.

Joe
 
Hi,
You have to declare it as Public in a standard module, not a class or form module.
 
Joe said:
I'm trying to publically define a dynamic array, arrSub() in the declarations
section of my code for a particular form. The array is designed to accept a
chaning number of string variables, and will be used by other forms and
modules throughout the running of my program.

The following code begins my module:

Option Compare Database
Option Explicit
Option Base 1

Public arrSub() As Variant

When I debug, I get an error message: "Constants, fixed-length strings,
arrays, user-defined types and Declare statements not allowed as Public
members of object modules."

The Help file gives the following advice: "Although a procedure can't return
an array, it can return a Variant that contains an array. To simulate a
Public array in a class module, use a set of Property procedures that accept
and return a Variant containing an array."

I'm not sure what to make of that, but I know that dynamic arrays can be
defined as public somehow. Does anyone have any ideas. Many thanks in
advance.


You can have Public arrays in a standard module, but not in
a class module.

If you must have the array in a form's class module, then
you have to make it Private and create procedures to manage
the array's size and access the array elements. These
procedure are then used as properties and methods of the
form.
 
Thank you both for your help. Now it seems I have another problem on my
hands. Is there any way to create a publically defined, dynamically created
array containing fixed-length strings?

For example:
(in standard module)
Public arrSub() as Variant

(in class module)
Private Sub CommandButton_Click()
ReDim arrSub("Item1", "Item2", "Item3")

I have a feeling there isn't, but I'd like to check. Has anybody
experienced similar limitations and found a way around them?

Again, thanks for the help.
 
Joe said:
Thank you both for your help. Now it seems I have another problem on my
hands. Is there any way to create a publically defined, dynamically created
array containing fixed-length strings?

For example:
(in standard module)
Public arrSub() as Variant

(in class module)
Private Sub CommandButton_Click()
ReDim arrSub("Item1", "Item2", "Item3")

I have a feeling there isn't, but I'd like to check. Has anybody
experienced similar limitations and found a way around them?


This is not a problem, it's just that it is not legal
syntax. Try this:

ReDim arrSub(3)
arrSub(1) = "Item1"
arrSub(2) = "Item2"
arrSub(3) = "Item3"
 
Thanks, Marsh. This is perfect!

Marshall Barton said:
This is not a problem, it's just that it is not legal
syntax. Try this:

ReDim arrSub(3)
arrSub(1) = "Item1"
arrSub(2) = "Item2"
arrSub(3) = "Item3"
 
Back
Top