Link to home
Start Free TrialLog in
Avatar of cocosteel
cocosteel

asked on

multidimensional arrays

-i want to create a dynamic array with 2 columns and variable amount of rows

-values will be assigned to column0 and column1 of the array within a for loop

-so at the end of each loop the rows for column0 and column1 need to be redimensioned by +1

-at a later stage i want to get the values from column0 and from column1 of the array



here is what i have written it may explain further

'******************************************

dim arr () as string

for i = 0 to 10
 
redim preserve arr(i,1)   ' ps - i dont think this will work
' create i number of rows and 2 columns

' assign item to column 1
' assign item to column 2
' move onto next row

next i

in a later bit of functionality

'******************************************

for i = 0 to 10


' get item from column 1 and display in message box
' get item from column 2 and display in message box
' move onto next row

next i

'******************************************

thanks
Avatar of AzraSound
AzraSound
Flag of United States of America image

In multidimension arrays, you can only dynamically resize the last dimension, so in your case, you can Redim Preserve arr(1, i), but you cannot Redim Preserve arr(i, 1)
ASKER CERTIFIED SOLUTION
Avatar of brother7
brother7
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ZedFX
ZedFX

do it the other way round

like so:


'******************************************

dim arr () as string

for i = 0 to 10
 
' create i number of rows and 2 columns
redim preserve arr(0 To 1, i)

' assign item to column 1
arr(0, i) = <item here>

' assign item to column 2
arr(1, i) = <item here>

' move onto next row
next i

'******************************************


then

'******************************************

for i = 0 to 10

' get item from column 1 and display in message box
MsgBox arr(0, i)

' get item from column 2 and display in message box
MsgBox arr(1, i)

' move onto next row
next i

'******************************************

enjoy :-)
yes brother7s way is more efficiant, if you size the array before hand it is better

'******************************************
dim arr (0 To 1, i) as string

for i = 0 to 10
 
' assign item to column 1
arr(0, i) = <item here>

' assign item to column 2
arr(1, i) = <item here>

' move onto next row
next i

'******************************************

then

'******************************************

for i = 0 to 10

' get item from column 1 and display in message box
MsgBox arr(0, i)

' get item from column 2 and display in message box
MsgBox arr(1, i)

' move onto next row
next i

'******************************************
 
Thanks guys!  I'm trying to self-learn VB.NET by reading Deitel's Visual Basic.NET How to Program.  Actually, I'm up to Chapter 7 which happens to be about arrays, so it's fresh in my mind.  Makes me feel good that I can use my newfound knowledge to help people here :)

Good luck to cocosteel on your project.
Avatar of cocosteel

ASKER

brother7 i am looking for a good vb.net book.  i bought complete visual basic.net and it is complete rubbish.
there are loads of syntax errors in the code examples.

do you recommend deitel's - i am a vb6 programmer (not very good!) looking to move to .NET
Deitel's Visual Basic.NET How to Program
http://www.amazon.com/exec/obidos/ASIN/0130293636/hawaiichesspr-20

The above book is what I'm currently reading and I really like it.  But I must warn you that it reads like a college textbook, so you sorta need an academic mindset.  It is NOT light reading, but it's comprehensive and well-written.  Before deciding to learn VB.NET, I had only done minimal progamming in BASIC in high school and a couple of courses in Pascal and FORTRAN as an undergrad.  I'm 37 now so that was about 20 years ago.  Reading this book has gotten me up to speed pretty quickly, at least with writing console applications.  I still haven't gotten to the OOP or GUI material yet, but so far, I've had no problems with the structured programming stuff.  I highly recommend this book.

There's another book by Deitel thats covers 95% of the same material as the first one, but is geared towards people who have some experience with VB6 already.  It might be more appropriate for you.  Here's the table of contents: http://www.deitel.com/books/vbnetFEP1/vbnetFEP1_toc.html

And a link to the book:
Deitel's Visual Basic.NET for Experienced Programmers
http://www.amazon.com/exec/obidos/ASIN/0130461318/hawaiichesspr-20

There's a substantial difference in price... $85 vs $38.50.  I think if I were you, I'd get the one for Experienced Programmers.  You get the same info but a little more condensed and without the glossy color pages that surely jack up the price.

OOP is totally new to me, so I got an additional book to help me along in that area.  It is:
OOP with Microsoft Visual Basic .NET and Microsoft Visual C# .NET Step by Step
http://www.amazon.com/exec/obidos/ASIN/0735615683/hawaiichesspr-20
I haven't cracked that one yet, so I can't make a solid recommendation about it.

But for sure, get one of the Deitel books.  Hope that helps.
brother7

i am using your idea of dimensioning an array with an estimate then at the end redimensioning it to remove and blank rows as suggested on Date: 09/20/2003 01:17PM PDT

but i am getting a "subscript out of range" error

here is the basic code

'************************************
Private Sub Command6_Click()
Dim arr() As String

ReDim Preserve arr(10, 1)

ReDim Preserve arr(5, 1) ' IT ERRORS HERE -

end sub
'************************************

can you redimension a multidimensional array?
if the second dimension stays the same but the first is changed?

thanks
Unfortunately, I came across this from another website:
"Only the upper bound of the last dimension in a multidimensional array can be changed when you use the Preserve keyword; if you change any of the other dimensions, or the lower bound of the last dimension, a run-time error occurs."


If you're still determined to use ReDim Preserve, you can try this.

'********************
' Private Sub Command6_Click()

Dim arr() as String
.
.
ReDim Preserve arr(1,10)
.
.
ReDim Preserve arr(1,5)

End Sub
'********************

It might look a bit funny, but I think it's a reasonable workaround to the problem.
Do any of you happen to know if you can set an array using variables like this?

#########################
Dim iNum As Integer = 0
While dataReader.Read()
                iNum += 1
End While
Dim myArray(iNum - 1, dataReader.FieldCount - 1) As String
#########################

I'm working with datasets that alwasy return a different number of rows AND columns depending on what a user requests. So if i can use variables to declare my array dimentions then the problem is solved for me. If i acutally have to change a physical number in the array declairation then i need to think of something else.
'to redimension a multi dimensional array, do the following:
initial declare
dim MyArray() As Integer
then to redim it
ReDim MyArray(0 To UboundA, 0 To UboundB) As Integer