Use a listbox as a dynamic array?

R

Robert Crandal

My VBA program will require multiple dynamic arrays of
strings. These will be "parallel arrays", meaning the arrays
will be the same size, and the indexes of each array will
be related to the corresponding indexes of the other arrays.

So....I decided that I do NOT want to bother using dynamic
arrays for my program, simply because dynamic arrays are
a pain to manage, and other reasons related to the design of
my program.

Instead, I was thinking about creating a few hidden Listbox
controls on a Userform (i.e Visible = False). Then, I could
simply use Listbox.AddItem() to continually add new strings
to the various hidden Listbox's, and possibly remove or
search for strings if necessary.

I'm just curious, but has anybody ever implemented something
like this in their VBA program? How did it work for you?
Were there any problems that I should know about?

What does everybody else think about this idea?

I'd appreciate any comments. Thank you.

Robert
 
W

witek

Robert said:
My VBA program will require multiple dynamic arrays of
strings. These will be "parallel arrays", meaning the arrays
will be the same size, and the indexes of each array will
be related to the corresponding indexes of the other arrays.

So....I decided that I do NOT want to bother using dynamic
arrays for my program, simply because dynamic arrays are
a pain to manage, and other reasons related to the design of
my program.

Instead, I was thinking about creating a few hidden Listbox
controls on a Userform (i.e Visible = False). Then, I could
simply use Listbox.AddItem() to continually add new strings
to the various hidden Listbox's, and possibly remove or
search for strings if necessary.

I'm just curious, but has anybody ever implemented something
like this in their VBA program? How did it work for you?
Were there any problems that I should know about?

What does everybody else think about this idea?

I'd appreciate any comments. Thank you.

Robert
I did.
it works well, however I can't tell anything about performance because
it was used for very small arrays.

By the way that is the only way to get empty two-dimensional array. (no
elements)
 
G

GS

Robert Crandal expressed precisely :
My VBA program will require multiple dynamic arrays of
strings. These will be "parallel arrays", meaning the arrays
will be the same size, and the indexes of each array will
be related to the corresponding indexes of the other arrays.

So....I decided that I do NOT want to bother using dynamic
arrays for my program, simply because dynamic arrays are
a pain to manage, and other reasons related to the design of
my program.

Instead, I was thinking about creating a few hidden Listbox
controls on a Userform (i.e Visible = False). Then, I could
simply use Listbox.AddItem() to continually add new strings
to the various hidden Listbox's, and possibly remove or
search for strings if necessary.

I'm just curious, but has anybody ever implemented something
like this in their VBA program? How did it work for you?
Were there any problems that I should know about?

What does everybody else think about this idea?

I'd appreciate any comments. Thank you.

Robert

I've ALWAYS found using dynamic arrays much easier to manage, AND get
way better performance than read/write to a control or worksheet. IOW,
your logic (IMO) for using hidden listboxes is flawed because that's
way more complex than just managing the arrays you populate them with.
You need to keep track of which listbox contains what data same as
you'd have to keep track of which array contains what data. Listbox
indexes start at zero; dynamic 2D arrays of ranges start at 1. This
adds to management complexity.

If you have a good naming convention in place then working with arrays
is trivial (so long as you understand array structures)!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
G

GS

witek used his keyboard to write :
By the way that is the only way to get empty two-dimensional array.
(no elements)

Not true! You can make any element 'empty' in any array, regardless of
its number of dims.

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
W

witek

GS said:
witek used his keyboard to write :

Not true! You can make any element 'empty' in any array, regardless of
its number of dims.


can you show me a code which creates array with 5 columns and zero rows?
 
G

GS

witek expressed precisely :
can you show me a code which creates array with 5 columns and zero
rows?

That's not an array! Show me otherwise...

Can you show a listbox that has 5 columns and zero rows? Yes.., when
it's empty. Can you load this into an array with zero rows? No!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
W

witek

GS said:
witek expressed precisely :

That's not an array! Show me otherwise...

so what is that?
What is the name of structure returned by listbox.list if listbox is empty?
Can you show a listbox that has 5 columns and zero rows? Yes.., when
it's empty. Can you load this into an array with zero rows? No!
as above . What does listbox.list returns?
 
R

Robert Crandal

GS said:
I've ALWAYS found using dynamic arrays much easier to manage, AND get way
better performance than read/write to a control or worksheet. IOW, your
logic (IMO) for using hidden listboxes is flawed because that's way more
complex than just managing the arrays you populate them with. You need to
keep track of which listbox contains what data same as you'd have to keep
track of which array contains what data. Listbox indexes start at zero;
dynamic 2D arrays of ranges start at 1. This adds to management
complexity.

I thought I read somewhere that it was not possible to resize an array that
already contains data. I could be wrong about this.

I was initially thinking about creating two or three separate arrays, each
containing sized at 6000 elements, and then add or delete data as necessary.
I was thinking this might be a slightly inefficient approach, so I
considered
the idea of using a Listbox as an array that can grow when necessary.

My program already requires one Listbox that will be visible. I thought
about creating one or two separate hidden Listbox controls that will
be parallel to the visible Listbox (meaning the Listboxes are the same size
and each index corresponds to each other according to my program).
It seemed like an easy to manage method to me, because all I need to do
is call AddItem() for each Listbox, one at a time, and it eliminated the use
of global arrays that require dynamic resizing and whatever.
 
G

GS

witek used his keyboard to write :
so what is that?
What is the name of structure returned by listbox.list if listbox is
empty?

as above . What does listbox.list returns?

Whatever are you on about? Your response to my statement of being able
to make any element of any array empty has nothing to do with that
statement! Please explain what your point is!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 
G

GS

Robert Crandal wrote on 07/03/2013 :
I thought I read somewhere that it was not possible to resize an
array that
already contains data. I could be wrong about this.

I was initially thinking about creating two or three separate arrays,
each
containing sized at 6000 elements, and then add or delete data as
necessary.
I was thinking this might be a slightly inefficient approach, so I
considered
the idea of using a Listbox as an array that can grow when necessary.

My program already requires one Listbox that will be visible. I
thought
about creating one or two separate hidden Listbox controls that will
be parallel to the visible Listbox (meaning the Listboxes are the
same size
and each index corresponds to each other according to my program).
It seemed like an easy to manage method to me, because all I need to
do
is call AddItem() for each Listbox, one at a time, and it eliminated
the use
of global arrays that require dynamic resizing and whatever.

A 1D array can be resized using ReDim. It can also be downsized using
Filter to remove empty elements.

A 2D array can be parsed of empty elements by putting it into another
array.

Either approach isn't really any different that what you're doing with
listboxes as far as the physical process goes. What is different,
though, is that you have the extra overhead of the controls PLUS the
code to manage them. As far as accessing items by row/col goes, an
array is a lot easier than a multi-column listbox. An array also
doesn't have the extra read/write overhead related to working with the
individual listbox items.

I'm not saying there's anything wrong with what you're doing. It's just
not the approach I would take!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top