PDA

View Full Version : Array Problem


Topgun
06-20-2002, 10:01 PM
Hi Programmers,

Umm I was reading my VB6 book and I was reading on Arrays. Well I saw something in it and it gave me a small project to do which turned out to be a major problem. It had to do with displaying 6 employee names in 6 text boxes. So this is the code I came up with.......

Dim Employee(5) As String
Dim txtEmployees As String
for txtemployess = txtname(0 to 5)
Employee(0) = "Name Here"
Employee(1) = "Name Here"
Employee(2) = "Name Here"
Employee(3) = "Name Here"
Employee(4) = "Name Here"
Employee(5) = "Name Here"
End Sub

As you can see I stopped here because while working with my loop, see I was makin a loop so I could assign txtName(0 to 5) to the variable txtEmployees, by the way txtName(0 to 5) are all text boxes that is why they are not declared as variables in my code. Well when I was making my Loop and when i put...... for txtemployees = txtname(0 to 5) and pressed enter to make a new line well I got an error message. And I don't understand why. My txtName(0 to 5) is a control array so I know that isn't the problem. What is the problem? Can someone help me?

Mark__C
06-23-2002, 02:57 AM
Well first off, a For...Next loop needs an integer to tell it how many times to loop.


Dim IntFor as Integer
For IntFor = 0 to 10 'Display 10 MessageBoxes Counting Up
Call MsgBox(CStr(I))
Next Intfor 'You dont need the IntFor after Next but It helps especially if you have nested For...Next Loops (Would get confusing)



If I understand what you are trying to do correctly, it could simply be done by the following code assuming you have already created the TextBox array:


Dim IntFor as Integer
Dim StrEmployees(5) as String

For IntFor = 0 to 5
txtName(IntFor).Text = StrEmployees(IntFor) 'I Didnt set the names so It wont return anything
Next IntFor


What you were trying to looked more like a For...Each loop which could also be used but For..Next is the appropriate one to use I think. Anyway, a For..Each loop would be used in the following manner:


Dim StrEmployees(5) as String
For Each TextBox in txtName
TextBox.Text = StrEmployees(TextBox.Index) 'I Didnt set the names so It wont return anything
Next


Assuming you already created the TextBox array.

As you can see, It's much better to use the For..Next loop.

Hope this helps.

leblitzer
08-24-2003, 04:04 PM
Originally posted by Mark__C
Well first off, a For...Next loop needs an integer to tell it how many times to loop.


Dim IntFor as Integer
For IntFor = 0 to 10 'Display 10 MessageBoxes Counting Up
Call MsgBox(CStr(I))
Next Intfor 'You dont need the IntFor after Next but It helps especially if you have nested For...Next Loops (Would get confusing)



If I understand what you are trying to do correctly, it could simply be done by the following code assuming you have already created the TextBox array:


Dim IntFor as Integer
Dim StrEmployees(5) as String

For IntFor = 0 to 5
txtName(IntFor).Text = StrEmployees(IntFor) 'I Didnt set the names so It wont return anything
Next IntFor


What you were trying to looked more like a For...Each loop which could also be used but For..Next is the appropriate one to use I think. Anyway, a For..Each loop would be used in the following manner:


Dim StrEmployees(5) as String
For Each TextBox in txtName
TextBox.Text = StrEmployees(TextBox.Index) 'I Didnt set the names so It wont return anything
Next


Assuming you already created the TextBox array.

As you can see, It's much better to use the For..Next loop.

Hope this helps.
i confirm it :)