Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Oct 1, 1996



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Dynamically Generating Variable Names

I have a number of objects, say shapes, which I have named s1, s2 , s3, s4 , s5. I would like to refer to the shapes fillcolor property with a routine such as FillColor(i,Color). I would like to create the following instruction:
I am unable to create the object name in this way. Can it be done? If so, how?

Unfortunately, you can't generate variable names to use in your code. However, if you use a control array, you can programmatically do what you want to. To make a control array, give all five of the controls the same name, and set each of their Index properties to a number in a sequence (1, 2, 3, 4, 5). Then, your code can look something like this:

For i = 1 to 5
   Shape(i).FillColor = Color
Next i
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date