How to get a loop an Array

Hi!

How did you count to the end of an array? for make a loop with For.
In a XMLnodeList exists the property “.Length” and uses as this mode:

For i As Integer = 0 To nodes.Length-1

Next.

Regards :smiley:

for i=0 to array.ubound

[quote=192237:@Dave S] for i=0 to array.ubound [/quote]
Thanks man, It Works!.

So I have one More question.

I have a “Conceptos” array with a lot of values.

So I want to display every value on the array in a Label.

I’m Using this:

lblConceptos1.text = “”
For i as Integer = 0 to Conceptos.Ubound
lblConceptos1.Text = lblConceptos1.text + EndOfLine+ Conceptos(i) + EndOfLine
Next

If I omit “lblConceptos.text”, it writes this: “------------------------------------”
So I think to put this “” in order to write nothing.

The issue is that It makes an extra endofline, like this:

Apples

Pears

Watermelons

Bananas

Grapes

And I wanna you like this mode:

Apples
Pears
Watermelons
Bananas
Grapes

What Am I doing wrong?

lblConceptos1.Text = lblConceptos1.text + Conceptos(i) + EndOfLine

Perfect, I was writing an extra EndOfLine.

One more question, How did you clear all the values of an Array?
Because I invoke the values of the Array to show it on the label.

But when I press again the button, when the Array has 1 value instead of two, It shows the new value(good) and the second(bad) from the last query.

Regards.

Reading the LangRef and Users Guide might be a good start

Redim resets an array back to empty

lblConceptos1.text = ""
For i as Integer = 0 to Conceptos.Ubound
lblConceptos1.Text = lblConceptos1.text + EndOfLine+ Conceptos(i) + EndOfLine 
Next

can be replaced with

lblConceptos1.text = join(Conceptos,EndOfLine)

[quote=192249:@Dave S]Reading the LangRef and Users Guide might be a good start

Redim resets an array back to empty

lblConceptos1.text = ""
For i as Integer = 0 to Conceptos.Ubound
lblConceptos1.Text = lblConceptos1.text + EndOfLine+ Conceptos(i) + EndOfLine 
Next

can be replaced with

lblConceptos1.text = join(Conceptos,EndOfLine) [/quote]
Yeah Ok I’m agree with you. The Issue is that If I redimension the elements of my Array I can’t put back more elements to my array.

When I make the query, sometimes the array “Conceptos” will have 2 elements, sometimes one value, sometimes 200 values, sometimes 3, sometimes one value, etc.

[quote=192249:@Dave S]Reading the LangRef and Users Guide might be a good start

Redim resets an array back to empty

lblConceptos1.text = ""
For i as Integer = 0 to Conceptos.Ubound
lblConceptos1.Text = lblConceptos1.text + EndOfLine+ Conceptos(i) + EndOfLine 
Next

can be replaced with

lblConceptos1.text = join(Conceptos,EndOfLine) [/quote]
Yeah, No I’m sorry.

I’m applying it wrong.

It works when I use it on my method,like this:

For i As Integer = 0 To nodes.Length-1
Redim Conceptos(i)
node = nodes.Item(i)
Conceptos(i) = "Cantidad: " + node.GetAttribute(“cantidad”) + " Unidad: " + node.GetAttribute(“unidad”) + " Descripción: " + node.GetAttribute(“descripcion”) + " valorUnitario: " + node.GetAttribute(“valorUnitario”) + " Importe: " + node.GetAttribute(“importe”)
next

When count 0 to the end of the items of the XMLNodeList
Before to assign values to the Array, first Redimension the Array by the elements existents in the NodeList.

Regards

You should use

[code]Redim Conceptos(-1)
For i As Integer = 0 To nodes.Length-1

node = nodes.Item(i)
Conceptos.Append "Cantidad: " + node.GetAttribute(“cantidad”) + " Unidad: " + node.GetAttribute(“unidad”) + " Descripcin: " + node.GetAttribute(“descripcion”) + " valorUnitario: " + node.GetAttribute(“valorUnitario”) + " Importe: " + node.GetAttribute(“importe”)
next

[/code]

Redim -1 sets the array to empty, then you append each item to the array making it grow as needed.