|
-
Dec 13th, 2003, 06:01 AM
#1
Thread Starter
Fanatic Member
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:
Dim CusArray(,)
Dim i, j As Integer
i = 0
j = 0
While MyReader.Read
CusArray(i, 0) = "1" 'MyReader("CustomerId")
CusArray(i, 1) = "Carl Blanchard" 'MyReader("FullName")
End While
ReDim Preserve CusArray(LBound(CusArray), UBound(CusArray))
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
-
Dec 13th, 2003, 06:10 AM
#2
Addicted Member
try setting initial values for each dimension when declaring the array
dim CusArray(1,1)
-
Dec 13th, 2003, 06:16 AM
#3
Thread Starter
Fanatic Member
OK Cool that worked however i get an error now on line
VB Code:
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
-
Dec 13th, 2003, 06:26 AM
#4
Addicted Member
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 )
-
Dec 13th, 2003, 06:32 AM
#5
Addicted Member
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
-
Dec 13th, 2003, 06:46 AM
#6
Thread Starter
Fanatic Member
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
-
Dec 13th, 2003, 07:20 AM
#7
Thread Starter
Fanatic Member
Nope not infact working
ok heres current code
VB Code:
Dim CusArray(0, 9)
Dim i As Integer
i = -1
While MyReader.Read
i = i + 1
'ReDim CusArray(i, 9)
CusArray(i, 0) = MyReader("CustomerId")
CusArray(i, 1) = MyReader("FullName")
CusArray(i, 2) = MyReader("Company")
CusArray(i, 3) = MyReader("BillingBuilding")
CusArray(i, 4) = MyReader("BillingStreet")
CusArray(i, 5) = MyReader("BillingStreet2")
CusArray(i, 6) = MyReader("BillingTown")
CusArray(i, 7) = MyReader("BillingCounty")
CusArray(i, 8) = MyReader("BillingPostCode")
CusArray(i, 9) = MyReader("BillingCountry")
'msgbox(cusArray(i,1))
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
-
Dec 13th, 2003, 07:45 AM
#8
Thread Starter
Fanatic Member
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
-
Dec 13th, 2003, 01:28 PM
#9
VB Code:
'not tested but it should :D work (too lazy to make a mockup of ur MyReader)
Dim colCustomers As Collection
Dim strArray(9) As String
'Dim CusArray(0, 9)
While MyReader.Read
strArray(0) = MyReader("CustomerId")
strArray(1) = MyReader("FullName")
strArray(2) = MyReader("Company")
strArray(3) = MyReader("BillingBuilding")
strArray(4) = MyReader("BillingStreet")
strArray(5) = MyReader("BillingStreet2")
strArray(6) = MyReader("BillingTown")
strArray(7) = MyReader("BillingCounty")
strArray(8) = MyReader("BillingPostCode")
strArray(9) = MyReader("BillingCountry")
'Add To Collection
colCustomers.Add(strArray)
End While
'Now Im guessing that you really need the data in a
'multi-dimetimetion array
Dim cusArray(colCustomers.Count - 1, 9)
Dim I As Integer
Dim X As Integer
For I = 0 To colCustomers.Count - 1
For X = 0 To 9
strArray = colCustomers.Item(I + 1)
cusArray(I, X) = strArray(X)
Next
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|