How do I use Arrays?

Hi All,

I'm a bit confused with the whole array business.

I'm trying to gather all the recordID's in an array after a search result so a user can navigate through it using array functions.

Now, after all the other code - I'd like to read the recordID into the array like so:

        Do While dr.Read()

              ' Add  dr.Item("RecordID") to  RecordIDArray()


How do I initialise an array I don't know the length of and how do I add values to it as this loop loops!?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi ITranger

In order to dimension an array you need to use the Array.CreateInstance method and that allows you to define 1, 2 or 3 dimension arrays. Below is a good link that explains it quire well with code examples

Once you have done that then in order to set an array element, you use

myArrary.SetValue(value, atIndex)

As you can see, in code you will determine at what array index position you want the value to apply.

A bit of noddy code but it might help...

        Dim arrTest As Array
        Dim dr As DataRow
        ' here i define the array as 100 elements of type integer
        arrTest = Array.CreateInstance(GetType(String), 100)  
        For Each dr In ds.Tables("Customers").Rows

            ' the autoNumber value from the access record is used to set the index position, but his could
            ' easily have been some other counter. The value within the array is the company name from the access record

            arrTest.SetValue(dr("CompanyName"), CType(dr("autoField"), Integer))


HTH :)

ITrangerAuthor Commented:
Thanks for your reply.

After some more reading I did this:

            'Read the results into an array
            Dim i As Integer = 0
            Dim RecordIDArray() As Long
            Do While dr.Read()

                ReDim Preserve RecordIDArray(i) '<--- ReDim and Preserve, this re-dimensions the array, preserving the stored values! :)
                RecordIDArray(i) = dr.Item("RecordID")
                i += 1


This seems to work well! Arrays seem so much more complex than that of PHP - prob because they're more powerful so best get reading :)

So which method do I use? I like the above as it's more like what I'm used to with a thrown in 'ReDim Preserve'!
Just use arraylists
you don't have to give size, it will dynamicaly set the size

 dim RecordIDArray as new arraylist
 Do While dr.Read()
              ' Add  dr.Item("RecordID") to  RecordIDArray()


That's all,

you can access the item as
RecordID(0), RecordID(1)
RecordID.item(0), RecordID.item(1), etc

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ITrangerAuthor Commented:
Ah cool,

I did try that but missed out the 'New' keyword - no wonder it didn't work!!

ArrayList it is then!!!

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.