Results 1 to 9 of 9

Thread: Multidimensional dynamic Arrays in vb.net

  1. #1

    Thread Starter
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539

    Multidimensional dynamic Arrays in vb.net

    Hi Everyone,

    Ive come across the need for a Multidimensional dynamic Array however its causing a headache so can anyone shed any light on the matter...

    VB Code:
    1. Dim CusArray(,)
    2.                 Dim i, j As Integer
    3.                 i = 0
    4.                 j = 0
    5.  
    6.                 While MyReader.Read
    7.                     CusArray(i, 0) = "1" 'MyReader("CustomerId")
    8.                     CusArray(i, 1) = "Carl Blanchard" 'MyReader("FullName")
    9.                 End While
    10.                 ReDim Preserve CusArray(LBound(CusArray), UBound(CusArray))
    11.  
    12.                 MsgBox(CusArray(0, 0))

    I get this error saying "Object reference not set to the instance of the object" which is caused my CusArray(i, 0) = "1" 'MyReader("CustomerId") However if i try and Dim CusArray(,) as New Array it dosnt allow me just says Arrays cant not be declared with New- ive read loads of posts on here but none help, most of them are vb6 and not .net,


    I will know the size of the size of the subscript which will be max of 11 however this array is made up from records out of my db so i will not know the size of the Index......


    Can any one help, code examples would be good

    thanks Carl
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

  2. #2
    Addicted Member
    Join Date
    Sep 2003
    Posts
    227
    try setting initial values for each dimension when declaring the array

    dim CusArray(1,1)

  3. #3

    Thread Starter
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539
    OK Cool that worked however i get an error now on line

    VB Code:
    1. CusArray(i, 1) = "Carl Blanchard" 'MyReader("FullName")

    Saying

    Index was outside the bounds of the array ???????
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

  4. #4
    Addicted Member
    Join Date
    Sep 2003
    Posts
    227
    anyway you cant redim a multidimensional array ( you can redim only the last dimenstio ), take a look at system.collections namespace, maybe those classes will make your life easier
    ( hashtable might work fine in your case )

  5. #5
    Addicted Member
    Join Date
    Sep 2003
    Posts
    227
    what i could think of is using a hashtable, the keys are your first dimenstion and add an arraylist object as the value for each key which will act like your second dimension,

    there might be better ways, it depends on how you want to retrieve the data and other factors

  6. #6

    Thread Starter
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539
    NOT WORKING READ THIS POST
    Last edited by carlblanchard; Dec 13th, 2003 at 11:40 AM.
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

  7. #7

    Thread Starter
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539

    Nope not infact working

    ok heres current code
    VB Code:
    1. Dim CusArray(0, 9)
    2.                 Dim i As Integer
    3.                 i = -1
    4.  
    5.                 While MyReader.Read
    6.                     i = i + 1
    7.                     'ReDim CusArray(i, 9)
    8.                     CusArray(i, 0) = MyReader("CustomerId")
    9.                     CusArray(i, 1) = MyReader("FullName")
    10.                     CusArray(i, 2) = MyReader("Company")
    11.                     CusArray(i, 3) = MyReader("BillingBuilding")
    12.                     CusArray(i, 4) = MyReader("BillingStreet")
    13.                     CusArray(i, 5) = MyReader("BillingStreet2")
    14.                     CusArray(i, 6) = MyReader("BillingTown")
    15.                     CusArray(i, 7) = MyReader("BillingCounty")
    16.                     CusArray(i, 8) = MyReader("BillingPostCode")
    17.                     CusArray(i, 9) = MyReader("BillingCountry")
    18.                     'msgbox(cusArray(i,1))
    19.                 End While

    here the problem,
    My DB is returning 3 records fine.

    However i get index out of range because - Dim CusArray(0, 9) is fixing the size to 1 index and 9 subscripts
    so incomes Redim CusArray(i,9) now i get with the message box.
    3 results 1 = blank 2 = blank 3 = Name
    This is because theres no preserve on the redim, however i cant redim the array with preserve because that only does the rightmost index - therefore im rowing up a stream without a paddle.........

    How can i make this array Dynamic ?????
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

  8. #8

    Thread Starter
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539
    i remember a friend of my when we were doing something liek this in ASP he managed to reverse it so instead of it being the last index that was preserved it would preserve the first one.

    he added something like reverse or rev when redimming the array. Any ideas ?
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

  9. #9
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    VB Code:
    1. 'not tested but it should :D work (too lazy to make a mockup of ur MyReader)
    2.  
    3.         Dim colCustomers As Collection
    4.         Dim strArray(9) As String
    5.         'Dim CusArray(0, 9)
    6.  
    7.         While MyReader.Read
    8.  
    9.             strArray(0) = MyReader("CustomerId")
    10.             strArray(1) = MyReader("FullName")
    11.             strArray(2) = MyReader("Company")
    12.             strArray(3) = MyReader("BillingBuilding")
    13.             strArray(4) = MyReader("BillingStreet")
    14.             strArray(5) = MyReader("BillingStreet2")
    15.             strArray(6) = MyReader("BillingTown")
    16.             strArray(7) = MyReader("BillingCounty")
    17.             strArray(8) = MyReader("BillingPostCode")
    18.             strArray(9) = MyReader("BillingCountry")
    19.  
    20.             'Add To Collection
    21.             colCustomers.Add(strArray)
    22.         End While
    23.  
    24.         'Now Im guessing that you really need the data in a
    25.         'multi-dimetimetion array
    26.  
    27.         Dim cusArray(colCustomers.Count - 1, 9)
    28.  
    29.         Dim I As Integer
    30.         Dim X As Integer
    31.  
    32.         For I = 0 To colCustomers.Count - 1
    33.             For X = 0 To 9
    34.                 strArray = colCustomers.Item(I + 1)
    35.                 cusArray(I, X) = strArray(X)
    36.             Next
    37.         Next
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

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