How do I use Arrays?

Posted on 2006-03-22
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()


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!?
Question by:ITranger
    LVL 6

    Assisted Solution

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


    Author Comment

    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'!
    LVL 9

    Accepted Solution

    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

    Author Comment

    Ah cool,

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

    ArrayList it is then!!!


    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now