Re: Array's

From: Varun (anonymous_at_discussions.microsoft.com)
Date: 02/01/04


Date: Sun, 1 Feb 2004 07:21:07 -0800

Hello Cor,

I have 2 arrays.
    Private CarArray As New ArrayList()
    Private NoOfCarSalesArray As New ArrayList()
    Private i As Intreger = 0

When i add the car name to the first array (CarArray), i also be add the quantity required in the NoOfCarSalesArray. They should then be on the same index number (i). They should always be the same length.

Here is my code for the button that adds these values to both the arraylists that i show u above.

            CarArray.Add(txtCaraName.Text)
            NoOfCarSalesArray.Add(txtCarQty.Text)
             i = (i + 1)

I then try to get values out of both these arraylist and put them both into a row in my datatable.
Dim max As Integer = CarArray.Count - 1
                Dim i As Integer
                For i = 0 To max
                    CarDTRow = ds.Tables("CarDT").NewRow()
                    CarDTRow ("CarReg") = txtCarReg.Text
                    CarDTRow ("CarMake") = CarArray(i)
                    CarDTRow("NoOfCarSales") = NoOfCarSalesArray(i)
                    ds.Tables("CarDT").Rows.Add(CarDTRow)
                Next Index

When i run this i get error:
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

when i swap the line
   Dim max As Integer = CarArray.Count - 1
to
   Dim max As Integer = NoOfCarSalesArray.Count - 1

I do not get this error and the coding application runs but i still get problems. All the values above are added to the datatable but not the ones that get values from the arraylist!!!
I do not know why.
Should i be doing some more coding? i am not sure if i have missed things out?