|
-
Jan 13th, 2006, 09:58 AM
#1
Thread Starter
Addicted Member
[RESOLVED] How can you do this without pointers?
I'm trying to do what C++ programmers would call a dynamically allocated 2d array. I don't know how big my rows or columns need to be beforehand. This is easy to do in C++ with pointers, but I don't know how to do it in VB.NET. I'm thinking along the lines of using an arraylist of arraylists, but I don't know how I can just keep creating new arraylists in a loop. Any suggestions on how to make a dynamic 2d array?
-
Jan 13th, 2006, 10:21 AM
#2
Re: How can you do this without pointers?
You can use an ArrayList of ArrayLists and using a loop to create multipe ArrayLists is done exactly as you would expect it to be:
VB Code:
Dim outerList As New ArrayList
For i As Integer = 1 To 10
outerList.Add(New ArrayList)
Next i
Given that ArrayLists are loosely-typed, I would actually recommend creating your own class that encapsulates all the collection manipulation and simply exposes methods like AddRow, AddColumn, Item etc. You could then maintain a strongly-typed collection of strongly-typed collections withing the instance of your class. If you are using .NET 2.0 you have Generics to allow you to specify the type at run time, but if you are using 1.x then you'd need to define your item type at compile time and inherit CollectionBase.
-
Jan 13th, 2006, 10:24 AM
#3
Re: How can you do this without pointers?
it may be slower in performace (a simple test could tell) but depending on what you are actually using this for, you may benefit from using an object like a datatable or something???
-
Jan 13th, 2006, 10:51 AM
#4
Thread Starter
Addicted Member
Re: How can you do this without pointers?
Thank you both. I will try to do the Arraylist of Arraylists and if I can't make it work I'll try a datatable or something like that. I'll let y'all know how it works out.
-
Jan 13th, 2006, 04:16 PM
#5
Thread Starter
Addicted Member
Re: How can you do this without pointers?
It worked beautifully using jmchilhiney's method. The part I was missing was missing was creating a new Arraylist for every row. I would also think that using a datatable would work very well.
-
Jan 13th, 2006, 04:18 PM
#6
Re: [RESOLVED] How can you do this without pointers?
Yeah I think so too, but like I said I don't know if there would be any performance drop using it instead of arraylists... you can always try if you have some spare time and post back the results of the 2 methods.
-
Jan 13th, 2006, 04:31 PM
#7
Thread Starter
Addicted Member
Re: [RESOLVED] How can you do this without pointers?
To test I would use a performance query counter thing I guess and just see how long each takes. (I can't remember what it is called right off, but I have some old code that uses that timer somewhere.) Is there a better way to test performance?
-
Jan 13th, 2006, 04:33 PM
#8
Re: [RESOLVED] How can you do this without pointers?
well it may just be an obvious noticable difference.. so you could test with that first..
for example, if you changed the code to use datatables, and it runs visibly WAY slower, then you know you are better off with arraylists, or vice versa...
if its too close to call, then yeah you would use performance counters or just do start and finish time checks to see how much time each loop took to complete
-
Jan 13th, 2006, 04:46 PM
#9
Thread Starter
Addicted Member
Re: [RESOLVED] How can you do this without pointers?
Just in case someone comes along later and wants this information, this is sort of how I used the 2d array although I cut out all the code that showed why it needed to be dynamic because it was a little hairy.
VB Code:
Dim lotDataArray as Arraylist
lotDataArray = new Arraylist
Dim i, j As Integer
For i = 0 To seqNumArray.Count - 1
lotDataArray.add(new Arraylist)
For j = 3 To dataReader.FieldCount - 1
lotDataArray.Item(i).add(dataReader.Item(j)) 'This is where the magic happens
Next
Next
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|