tags:

views:

51

answers:

5

I'm not sure how to make concatenate inside a loop in the same way as this:

For x As Integer = 1 to 10
    Me.Button & x & .top = 0
Next

instead of:

Me.Button1.top = 0
...
Me.Button10.top = 0

Any suggestions would be much appreciated, Thanks :)

+3  A: 

I recommend an array of buttons, my friend :)

It's been a while since I've done VB, but something like:

Dim buttons(10)
buttons(1) = Me.Button1
' Add other buttons here

For x As Integer = 1 to 10
  buttons(x).top = 0
Next

Better yet, if you are creating your buttons dynamically, just store the array of buttons instead of each button as a member of the form object.

zourtney
A: 

To do this in VB you would have to add each of the buttons to a collection and then loop over the collection. Non dynamic languages don't have very many conventions for looping over variable variable names.

wllmsaccnt
+3  A: 

Maybe you can do something like this:

For x As Integer = 1 to 10 
    Me.FindControl("Button" & x).top = 0 
Next 
Gabe
Interesting trick. This solution is actually closer to what was asked for, even if it's not necessarily considered the best programming practice. It has always been my assumption that this sort of dynamic name look is a costly operation -- do you know how well it performs?
zourtney
The name lookup is just a dictionary operation, so it's pretty fast. It's certainly minimal compared to the cost of the rerendering required by the property change.
Gabe
A: 

Or... no array

dim ButtonPrefix as string = "Button" 
dim myButton as Button
For x as Integer = 1 to 10
   myButton = me.FindControl(ButtonPrefix & x)
   myButton.top = 0
Next

Also, top isn't a valid member of Button. Did you mean text?

drooksy
+1  A: 
 Dim buttonarray(10) As Button
 Dim x As Integer

 buttonarray(0) = Button1
 buttonarray(1) = Button2
 'Etc

  For x = 0 To 10
     buttonarray(x).Top = 0
  Next
Mark Hall