Jump to content

recursive object assignment to array elements?

- - - - -

  • Please log in to reply
4 replies to this topic

#1
compgeek97

compgeek97

    Newbie

  • Members
  • Pip
  • 6 posts
is there a way i can use loops to replicate something like this:

(btn0 to btn8 are buttons on my form)

dim buttons(8) as button


button(0) = btn0

button(1) = btn1

button(2) = btn2

button(3) = btn3

button(4) = btn4

button(5) = btn5

button(6) = btn6

button(7) = btn7

button(8) = btn8

with something like this:

for i as integer = 0 to 8

button(i) = btni 'maybe "btn & i" ?

next

this could get tedious if there were a lot of items to be assigned. i want to do this so that it would be easier to sort through them because its easy to use a for loop and put the counter as the element number.

i am using vb .net pro 2008 at home and pro 2003 at school for the class I'm taking.

#2
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 232 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
If memory serves, I think you have control arrays in VB6.

Simplify your VB6 coding with control arrays | TechRepublic

#3
compgeek97

compgeek97

    Newbie

  • Members
  • Pip
  • 6 posts
sorry for my ignorance but does this still work in either vb .net 2003 or 2008?

#4
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 232 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
No, not the same way. Control Arrays do not exist in .NET, the way they are in VB6.

You can get all controls, by getting the list of child Controls on the form.

Something like this:

Dim ctl As Control
For Each ctl In Me.Controls
If (TypeOf cControl Is TextBox) Then
' Add to your control list
End If
Next cControl

Not real code, but an example.

#5
compgeek97

compgeek97

    Newbie

  • Members
  • Pip
  • 6 posts
thank you, that works great! now i only wish there were still a way to do it at designtime :glare:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users