Results 1 to 9 of 9

Thread: [RESOLVED] How can you do this without pointers?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    230

    Resolved [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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim outerList As New ArrayList
    2.  
    3. For i As Integer = 1 To 10
    4.     outerList.Add(New ArrayList)
    5. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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???

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    230

    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.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    230

    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.

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    230

    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?

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    May 2005
    Posts
    230

    Post 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:
    1. Dim lotDataArray as Arraylist
    2.         lotDataArray = new Arraylist
    3.  
    4.         Dim i, j As Integer
    5.         For i = 0 To seqNumArray.Count - 1
    6.             lotDataArray.add(new Arraylist)
    7.             For j = 3 To dataReader.FieldCount - 1
    8.                 lotDataArray.Item(i).add(dataReader.Item(j))                 'This is where the magic happens
    9.             Next
    10.         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
  •  



Click Here to Expand Forum to Full Width