Array's

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello

I messaged before about arrays and got help and example off Cor. Here it is

Dim myArray As New ArrayLis
myArray.Add("Peugeot"
myArray.Add("Ferari"
myArray.Add("Fiat"
myArray.Add("Renault"
myArray.RemoveAt(0
myArray.Insert(1, "Mercedes"
myArray.Clear(

I want to ask how i can loop through an arraylist so i can get these values into another control
I tried this but it gets error

Private CarArray as new ArrayLis
Private Index as intege

Button1_Onclick..................................................
CarArray.add(Index, Textbox1.Text
Index = Index +

why can i not do this coding
thank yo
 
What do you want to do ?

The ArrayList.Add - method has got only one signature which takes only one parameter as being the object to be added to the list.
 
Hi Varun,

Diner time here but a quick one.

You better can add it to a listbox, but to show you the loop to add the
array to a textbox, while I am in a hurry

\\\
dim i as integer
for i = 0 to myarray.length - 1
mytextbox.text = mytextbox.text & myarray(i).tostring & vbcrlf
next
///

I hope this helps,

If there is an error, I did it really very quick.

:-))

Cor
 
Hello,

what i want to do is loop through the arraylist so that i can add all the values contained in this array into a datatable.

I used a simple array before with the following code to loop through my array and add values to a datatable:

For Index = 0 To (Index - 1)
CarDTRow = ds.Tables("CarDT").NewRow()
CarDTRow ("CarReg") = txtCarReg.Text
CarDTRow ("CarMake") = CarArray(Index)
ds.Tables("CarDT").Rows.Add(CarDTRow)
Next Index

I wanted to empty my array when the values were added to the datatable but i was told that i couldnt and that i would need to use a arraylist!!

Now that i have to use the arraylist, i am no longer sure on how to loop through my array the way i did when i use just the normal Array (not arrayList). Can u help me with this? Is this possible?
 
Thx Cor

I have tried this but i get error that sayin
'length' is not a member of 'System.Collections.ArrayList

i did this
Dim CarArray as new Arraylis
Dim i As Intege
For i = 0 To CarArrayList.length -
' ...................
Nex

the 3rd line of the coding above is highlited as problem code. (CarArrayList.length - 1 is highlighted)

Can anyone advise me
Thank you.
 
Hi,

i try using .count but i get an error that says:

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.
I have coded this:
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(Index)
CarDTRow("NoOfCarSales") = NoOfCarSalesArray
ds.Tables("CarDT").Rows.Add(CarDTRow)
Next Index

The code breaks on this line:
CarDTRow("NoOfCarSales") = NoOfCarSalesArray

The first array seems to work ok.
Very confused!!?
 
Hi,

i try using .count but i get an error that says:

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.
I have coded this:
Dim max As Integer = MyMealTypeArray.Count - 1
Dim i As Integer
For i = 0 To max
CarDTRow = ds.Tables("CarDT").NewRow()
CarDTRow ("CarReg") = txtCarReg.Text
CarDTRow ("CarMake") = CarArray(Index)
CarDTRow("NoOfCarSales") = NoOfCarSalesArray
ds.Tables("CarDT").Rows.Add(CarDTRow)
Next Index

The code breaks on this line:
CarDTRow("NoOfCarSales") = NoOfCarSalesArray

The first array seems to work ok.
Very confused!!?
 
Hi

i try using .count but i get an error that says

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dl
Additional information: Index was out of range. Must be non-negative and less than the size of the collection
I have coded this
Dim max As Integer = CarArray.Count -
Dim i As Intege
For i = 0 To ma
CarDTRow = ds.Tables("CarDT").NewRow(
CarDTRow ("CarReg") = txtCarReg.Tex
CarDTRow ("CarMake") = CarArray(i
CarDTRow("NoOfCarSales") = NoOfCarSalesArray(i
ds.Tables("CarDT").Rows.Add(CarDTRow
Next Inde

The code breaks on this line
CarDTRow("NoOfCarSales") = NoOfCarSalesArra

The first array seems to work ok.
Very confused!!
 
Hi Varun,

Sorry for that lenght answer, I do not know how it happens that I did that,
because mostly I try to be secure not to mix that up.

But luckely you got your answer from TC mc Queen.

The error is now I think in,
CarDTRow("NoOfCarSales") = NoOfCarSalesArray(i)

What is that array NoOfCarSalesArray.

When it is the same lenght of the other one it should go, but I doubt that.

Cor
 
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?
 
Hi Varun,

Are you using VS.net or VB.net with an IDE?

If not add this instruction and look what is the result when you start.
(But look further I have also something else that is strange)

Before this line
messagebox.show(CarArray.count.tostring & "+" &
NoOfCarSalesArray.count.tostring)
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
Make from this

Next

There is not integer Index in the loop it is i but that is automaticly taken
in this case.

Cor
 
Hello Cor,

That was a very clever of way performing a check. Thanku for that. I found that all values were added to the first array and non to the second.
I then change the addbutton_Onclick event to make it work properly. Thank you so much for your time and patience with me.
Im hoping now the problem is fix.

thank you
 
Back
Top