简体   繁体   中英

How do I increment the name of a variable e.g. bar1, bar2, bar3, VBNET in a For loop?

I'm trying to simplify my code:

        bar_x(0) = delocateX(bar1.Left)
        bar_y(0) = delocateY(bar1.Top)
        bar_x(1) = delocateX(bar2.Left)
        bar_y(1) = delocateY(bar2.Top)
        bar_x(2) = delocateX(bar3.Left)
        bar_y(2) = delocateY(bar3.Top)
        bar_x(3) = delocateX(bar4.Left)
        bar_y(3) = delocateY(bar4.Top)

which continues until 29, and I was trying to use a For loop to reduce the number of lines and simplify the code. This is my attempt:

     Function set_bar_positions()
         Dim i As Double
         For i = 0 To 29
             bar_x(i) = delocateX(("bar" & (i + 1)).Left)
             bar_y(i) = delocateY("bar" & (i + 1) + ".Top")
         Next

How can I simplify this Function?在此处输入图片说明

The fact that you're using Left and Top suggests that those variables refer to controls. If that is the case, you can access controls by name via the Controls collection of the parent. Assuming they are controls added directly tot he form in the designer:

bar_x(i) = delocateX(Controls("bar" & (i + 1)).Left)
bar_y(i) = delocateY(Controls("bar" & (i + 1)).Top)

Note that this is not working with variables. It is just conveniently the case that, when you add a control to a form in the designer, the field created has the same name as contained in the Name property of the control.

In a more general sense of what is alluded to in jmc's answer; you can do yourself what the forms designer did for you- it essentially added all your bars to a collection indexed by string

You can do the same, and make it easier, in your constructor:

Private bars() as New Control 'make an array of controls


'In the constructor
Sub New(...)

  'If you have 29 bars
  ReDim bars(28)
  bars(0) = bars1
  bars(1) = bars2
  ...

Remember that arrays start from 0, so you'll either need to do a bit of math when you want bars1, because in the array it's at index 0, or you can run your array to have 30 elements, put bars1 in bars(1) etc and just ignore the first element of the array

Remember that this establishes a new reference to the existing object; you can change anything about the object itself such as bars1.Left = 100 and bars(1) will see the change but if you change out bars1 for a whole new object then bars(1) will stay pointing to the old object and will need changing too

I managed to find the easiest solution to be:

Function set_bar_positions()
        For i = 0 To 29
            Dim cs = Me.Controls.Find("bar" & i.ToString(), True)
            If cs.Any() Then
                Dim c = cs.First()
                bar_x(i) = delocateX(c.Left)
                bar_y(i) = delocateY(c.Top)
            End If
        Next
    End Function

As answered by djv

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM