We help IT Professionals succeed at work.

How do I use Arrays?

ITranger
ITranger asked
on
Medium Priority
285 Views
Last Modified: 2010-04-23
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()

            Loop

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!?
Comment
Watch Question

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

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemarrayclasscreateinstancetopic.asp

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))

        Next


HTH :)

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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

            Loop

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'!
Commented:
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()
             RecordIDArray.add(dr.Item("RecordID"))
              ' Add  dr.Item("RecordID") to  RecordIDArray()

            Loop

That's all,

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

Author

Commented:
Ah cool,

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

ArrayList it is then!!!

Thanks
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.