473,657 Members | 2,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

VB dynamic arrays as lists

Hello all,

I want to perform the following task:

1. Create an array
2. Fill it up (number of elements unknown in advance)
3. Iterate through it using For Each loop (cannot do this in the in the
previous step)

I ended up with something like this:

1 Dim arr()
2 ReDim arr(0)
3 Do While <Loop Condition>
4 'Data acquisition here
5 ReDim Preserve arr(UBound(arr, 1) + 1)
6 arr(UBound(arr, 1) - 1) = theData
7 Loop
8 ReDim Preserve arr(UBound(arr, 1) -1)
...
9 For Each item In arr
10 'First data processing here
11 Next
...
12 For Each item In arr
13 'Second data processing here
14 Next

What I don't like:
- Have to redim every time a data is added to the array (line 5)
- Must have a dummy element (line 2) to make UBound happy
- Must drop the dummy element to make my for each simpler (line 8)
- Indices are ugly (lines 5, 6, and 8)

Could someone propose a better solution?
Nov 21 '05 #1
4 1465
"mom_newbie " <mo*******@disc ussions.microso ft.com> schrieb:
I want to perform the following task:

1. Create an array
2. Fill it up (number of elements unknown in advance)
3. Iterate through it using For Each loop (cannot do this in the in the
previous step)


I suggest to use an 'ArrayList' object instead of the array. 'ReDim
Preserve' is a costly operation that should be avoided.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2
In article <A1************ *************** *******@microso ft.com>, mom_newbie wrote:
Hello all,

I want to perform the following task:

1. Create an array
2. Fill it up (number of elements unknown in advance)
3. Iterate through it using For Each loop (cannot do this in the in the
previous step)

I ended up with something like this:

1 Dim arr()
2 ReDim arr(0)
3 Do While <Loop Condition>
4 'Data acquisition here
5 ReDim Preserve arr(UBound(arr, 1) + 1)
6 arr(UBound(arr, 1) - 1) = theData
7 Loop
8 ReDim Preserve arr(UBound(arr, 1) -1)
...
9 For Each item In arr
10 'First data processing here
11 Next
...
12 For Each item In arr
13 'Second data processing here
14 Next

What I don't like:
- Have to redim every time a data is added to the array (line 5)
- Must have a dummy element (line 2) to make UBound happy
- Must drop the dummy element to make my for each simpler (line 8)
- Indices are ugly (lines 5, 6, and 8)

Could someone propose a better solution?


Well... You have a couple of choices here.

1. Use an ArrayList
2. Use an Array

There are pro's and con's to both of these choices.

With the arraylist, you don't need to worry about the number of elements.
The ArrayList will grow as needed. The disadvantages to this approch is
that ArrayList stores it's elements as type System.Object. That means that
if you are storing value types (structures, integers, etc.) in the
ArrayList, then you have to deal with the performance hit of
boxing/unboxing on storage and retrival. This can be quite significant
as the list gets large. Where that cutoff is depends on your system, so
you may want to test this method and see if the performance is adequate.
If your types are Reference types (classes), then this not as big an
issue - but you do have to cast your objects back to the correct type on
retrieval. This is largely a non-issue in VB.NET 2005, with the
inclusion of generics.

With arrays, you have the issue of being limited to a fixed size. In
other words, you have to know up front how many elements you will store,
the tradeoff is that you will get much better performance when working
with large numbers of Value types. That said, there are ways around the
fixed size issue - using ReDim Preserve - and still maintain decent
performance. The cost is complexity. The way really to use arrays in
your above example is to NOT ReDim on every addition. The normal way is
to grow the array when ever you hit the number of elements. This is
actually the way arraylist works internally. It starts with enough
storage to hold 16 elements, but when you add the 17, it will basically
do a redim preserve on it's array and double the current storage. Of
course, then you have to have a way to keep track of the last element
you inserted etc. This would probably be best done inside of a custom
typed collection.

As stated, the methods you chose really depend on
1) the number of likely elements
2) the types of the data you are storing (Value vs. Reference types)

The only way to really know is to implement it, and see. Personally, I
would use the ArrayList first - and if you have any speed issues,
profile the app. If you find that it is your bottleneck, then you can
most likely speed things up with a custom collection implementation,
using an array that INTELLIGENTLY resizes as needed.

--
Tom Shelton [MVP]
Nov 21 '05 #3
I'm sorry if this is insulting, but its like 'arrays as lists' = arraylist

this is just so ironic rofl

mom_newbie wrote:
Hello all,

I want to perform the following task:

1. Create an array
2. Fill it up (number of elements unknown in advance)
3. Iterate through it using For Each loop (cannot do this in the in the
previous step)

I ended up with something like this:

1 Dim arr()
2 ReDim arr(0)
3 Do While <Loop Condition>
4 'Data acquisition here
5 ReDim Preserve arr(UBound(arr, 1) + 1)
6 arr(UBound(arr, 1) - 1) = theData
7 Loop
8 ReDim Preserve arr(UBound(arr, 1) -1)
...
9 For Each item In arr
10 'First data processing here
11 Next
...
12 For Each item In arr
13 'Second data processing here
14 Next

What I don't like:
- Have to redim every time a data is added to the array (line 5)
- Must have a dummy element (line 2) to make UBound happy
- Must drop the dummy element to make my for each simpler (line 8)
- Indices are ugly (lines 5, 6, and 8)

Could someone propose a better solution?

Nov 21 '05 #4
Obviously I posted this question to the wrong newsgroup.
I am writing a VBScript, not a VB program. Sorry.
Thanks everybody for the suggestions.
Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
3651
by: Troll | last post by:
Hi, I need to write a script which reads some data and reports the findings. Just to give you an idea the structure is similar to the following. Data input example: HEADING 1 ********** ColumnA ColumnB ColumnC ColumnD ColumnE
5
2546
by: Cant Think Today | last post by:
I have multi-dimesional arrays that can be specifed by the user, e.g 1,2,3,4,5 1,2,3,4,5,6,7,8,9,10 1,2,3,4,5,6 I think a bit of code that will iterate over these arrays to print out the element indices for each unique element in the N-dimensional array. E.g. for the above
11
2276
by: Soeren Sonnenburg | last post by:
Hi all, Just having started with python, I feel that simple array operations '*' and '+' don't do multiplication/addition but instead extend/join an array: a= >>> b= >>> a+b
15
2379
by: Ronny Mandal | last post by:
Assume that you want to store n records in an array, and that the array can hold all n elements, that is, it is declared int a. And suddenlny in run-time, I need to store n+m items. Why cannot this be done: a = e(0) a = e(1) ... ...
2
1972
by: taras.di | last post by:
Hi everyone, I've been reading up on how to create a drop down box who's context is dynamically produced based on the value of a previous select box. I've read a lot about some of the browsers not properly readjusting the width of a dynamically produced drop down box, and some browsers having problems adjusting the height and widths (most notably NN). Most solutions involve manually refreshing the browser window and/or padding the...
4
2465
by: learnfpga | last post by:
Here is a little code I wrote to add the numbers input by the user.....I was wondering if its possible to have the same functionality without using dynamic arrays.....just curious..... //trying to get input from the user to add all the numbers that user inputs //tried to do it without dynamic memory usage but probably cannot achieve it //here is using "new" and "delete" operator....
4
2173
by: bei | last post by:
Hi, I am trying to write several arrays into one file, with one arrays in one column. Each array (column) is seperated by space. ie. a= b= c= 1 5 9 2 6 10 3 7 11 4 8 12
3
2488
by: john | last post by:
I am VERY new to C++ (1st semester) put not so new to coding and programming (SQL, VB, etc). We are learning about classes. I have a program that needs to call many instances (i think this is the right term) of a class. What I have been taught so far is to write to a class we would do something like this in the main.cpp: a1.setmanSalary(something);
1
4315
by: Erland Sommarskog | last post by:
I am glad to announce that there is now a version of my article "Arrays and Lists in SQL Server" for SQL 2005 available on my web site. THe URL for the article is http://www.sommarskog.se/arrays-in-sql-2005.html. If you are curious about the performance numbers they are in an appendix at http://www.sommarskog.se/arrays-in-sql-perftest.html. The old version of the aritlce remains, as the new article covers SQL 2005 only. The old...
0
8850
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8523
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8626
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6178
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2749
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.