• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

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!?
  • 2
2 Solutions
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
ITrangerAuthor Commented:
Ah cool,

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

ArrayList it is then!!!


Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now