newbie: creating arrays without knowing upperbounds

V

vbDavidC

Hi,

I am wondering what the best way to define an array is if you don't
know how many elements should be in the array:

I created the following array, I initially set it to 10 but got an
error when I went over so I defined it to 50 cause I am certain 50
will not be reached.

dim lastname(50) as string

I am populating this array from my database with a data reader. After
I put the results into an array I REDIM PRESERVE with the correct
number, but I am wondering if there is a smarter way of doing it.

I also ask if creating the array like this can be wasteful. For
example if I were to create an int32 array does each element get
stored with x bytes, even if the element is empty?
 
N

Nate

I created the following array, I initially set it to 10 but got an
error when I went over so I defined it to 50 cause I am certain 50
will not be reached.

You can use the ReDim command. Set a MAX_ARR constant to some upper
bound, then every time you add to the array first check its length
against MAX_ARR. If the array gets to that size, do a

ReDim Preserve lastname(lastname.Length + 10)

(replacing 10 with some number you're comfy with.) This will
automatically enlarge your array any time it's necessary, but not
every time you add one element.
I also ask if creating the array like this can be wasteful. For
example if I were to create an int32 array does each element get
stored with x bytes, even if the element is empty?

If there was one thing my best CSci prof and former boss wouldn't
agree on, it was this. In college it was important to keep the code
and data storage as tight as possible. As soon as I graduated and got
a real job, elegance went out the window in favor of getting the job
done. I think it should depend on the project, your project lead, and
the code reviewers.

Have fun!
Nate
 
V

vbDavidC

You can use the ReDim command. Set a MAX_ARR constant to some upper
bound, then every time you add to the array first check its length
against MAX_ARR. If the array gets to that size, do a

ReDim Preserve lastname(lastname.Length + 10)

(replacing 10 with some number you're comfy with.) This will
automatically enlarge your array any time it's necessary, but not
every time you add one element.


If there was one thing my best CSci prof and former boss wouldn't
agree on, it was this. In college it was important to keep the code
and data storage as tight as possible. As soon as I graduated and got
a real job, elegance went out the window in favor of getting the job
done. I think it should depend on the project, your project lead, and
the code reviewers.

Have fun!
Nate

Nate, thanks for the info. As a matter of fact I was considering
redim-ing it after each element but thought that would be somehow not
a good design, now that you mention this it seems like an ideal
solution.
 
V

vbDavidC

Why does it have to be an array? Why can't you use a generic list?

Robin S.

Thanks for the reply. I used lastname as an example I have several
arrays I am using. I have to admit I don't know what a generic list
is.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Nate said:
You can use the ReDim command. Set a MAX_ARR constant to some upper
bound, then every time you add to the array first check its length
against MAX_ARR. If the array gets to that size, do a

ReDim Preserve lastname(lastname.Length + 10)

(replacing 10 with some number you're comfy with.) This will
automatically enlarge your array any time it's necessary, but not
every time you add one element.

Be aware though, that the ReDim command doesn't actually resize the
array. It creates a new array and copies the values from the old array.

Dynamic lists use arrays internally, and grow by creating a new larger
array. To keep the overhead to a minimum, they usually create an array
that is twice the size of the old array.
If there was one thing my best CSci prof and former boss wouldn't
agree on, it was this. In college it was important to keep the code
and data storage as tight as possible. As soon as I graduated and got
a real job, elegance went out the window in favor of getting the job
done. I think it should depend on the project, your project lead, and
the code reviewers.

So true. It's good to keep an eye on how expensive different methods
are, but it's not always the most sophisticated ones that are the most
efficient. Whatever works, works.

Allocating a few kilobytes of memory is not a big deal nowadays, as long
as you don't hold on to the memory that you don't need any longer than
you have to. The memory management in .NET is designed for objects to be
short lived, so a big object throughput is normal for a program.
 
F

ft310

Hi,

I am wondering what the best way to define an array is if you don't
know how many elements should be in the array:

I created the following array, I initially set it to 10 but got an
error when I went over so I defined it to 50 cause I am certain 50
will not be reached.

dim lastname(50) as string

I am populating this array from my database with a data reader. After
I put the results into an array I REDIM PRESERVE with the correct
number, but I am wondering if there is a smarter way of doing it.

I also ask if creating the array like this can be wasteful. For
example if I were to create an int32 array does each element get
stored with x bytes, even if the element is empty?

I just read this thread -- is there some reason the count of the items
being read is not determined before doing the reading. With this count
in hand then use that value in the dim statement.
 
V

vbDavidC

I just read this thread -- is there some reason the count of the items
being read is not determined before doing the reading. With this count
in hand then use that value in the dim statement.

I thought about generating a separate SQL command to give me the
number, dim the array, and then run my SQL command to get data for
array. I was thinking about that as a option but that was one reason
for asking the question.
 
C

Cor Ligthert [MVP]

Robin could have written as well a List instead of a Generic List.

There are all kinds of specialized Lists and collections in Net. They all
have implemented IList or/and Icollections, that makes them in a high order
interchangeable.

The most simple one is probably the arraylist. A list only in the form of an
array.

Dim myArray as new ArrayList
myArray.Add(AnnyItemAsObject).

Working with an object is however not pleasant (you have forever to cast),
therefore you can make it Generic by first declaring a class.

Than you can put that class in your ArrayList
dim myArray as new List (of MyClass).

Cor
 
F

ft310

I thought about generating a separate SQL command to give me the
number, dim the array, and then run my SQL command to get data for
array. I was thinking about that as a option but that was one reason
for asking the question.

Its what I do -- one of the secrets to successful programming is
deciding who is in control. The options generally are you or the data.
I've found data to be somewhat unreliable.

F.
 
B

Branco Medeiros

vbDavidC wrote:
Thanks for the reply. I used lastname as an example I have several
arrays I am using. I have to admit I don't know what a generic list
is.
<snip>

A generic list as suggested by Robin is by far the best general
approach (IMXHO). If you're using VB 2005, a generic list can be
declared like this:

Dim LastName As New List(Of String)

With that you can just have

LastName.Add(NewValue)

at each loop cicle, without having to consider the resizing that would
go on behind the scenes.

A generic list will double its capacity automatically whenever it
reaches its limit. It's been shown in practice that a factor of two is
the best multiplying value one can use when resizing a collection to
hold an unknown amount of future insertions (some people argue that a
better one would be 2.5, but I'll be conservative here).

After you finish adding new items you can call

LastName.TrimExcess

So the unused capacity is discarded.

If you're unfortunate enough to not be using VB 2005, then you can try
ArrayList, a dinamic list of Objects (in this case there would be some
casting whenever you wanted to retrieve an element from the list).

In this case, if you're the brave type or is learning the language,
why don't you roll your own? (using an array for storage and keeping
an eye in its capacity -- doubling it whenever more room was needed --
etc)

HTH.

Regards,

Branco.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

ft310 said:
Its what I do -- one of the secrets to successful programming is
deciding who is in control. The options generally are you or the data.
I've found data to be somewhat unreliable.

It's not always possible to securely or efficiently obtain the number of
objects. If there are any possibility that the number of records in a
table can change, you would have to use a transaction to lock the file
to securely get the record count before getting the data. You can get
control over the data, but that will make the operation much less efficient.
 
F

ft310

Its what I do -- one of the secrets to successful programming is
deciding who is in control. The options generally are you or the data.
I've found data to be somewhat unreliable.

F.

While Goran has an interesting point, in general it is best to obtain
the size of the population. In those rare instances where this is not
possible, reflection on alternative methods is usually called for
before coding begins.

F.
 

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