I want to create a function to handle a dynamic array of controls (labels, pushbuttons,…). The code I used is (Label1 is a hidden control on the window)
Dim MyLabel() As Label
This code works well when use a constant in the Dim instruction (e.g. Dim MyLabel(10) as Label). When I make MyLabel dynamic then there is no error or warning during the compilation process, but the program crashes during runtime. Any idea what I am doing wrong?
You can not declare a control set in code, you do it in the IDE.
- Select the control on the window and in the properties inspector choose in the “Member Of” popup “New Control Set”.
- Then change your code as follows:
Dim MyLabel As Label // remove the parentheses, you are not declaring an array here
MyLabel = new Label1 // remove the subscript operator
MyLabel.text = "Number" // remove the subscript operator
thank you for your reply. This works… But, this function will be used to receive a dynamic array of strings that I want to put them in MyLabel(0), MyLabel(1)… This makes it easy to fill the text properties by using a loop. So, what is wrong in my code?
Control sets are not really arrays (that’s why there were renamed from “Control Array” to “Control Set” during the change from REALbasic to Xojo). You can’t trust that the index numbers are 0…n, they could be anything (though usually they are 0…n). There is also no upper bound (Ubound is not working). You have to keep track yourself if you need the number and want to access them as array elements.
For i As Integer = 0 To myDynamicArray.Ubound
Dim MyLabel As Label
MyLabel = new Label1
MyLabel.text = myDynamicArray(i)
MyLabels.Append(MyLabel) // MyLabels being a property of the window declared as: MyLabels() As Label1