devxlogo

Dynamically Generating Variable Names

Question:
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 witha routine such as FillColor(i,Color).I would like to create the following instruction:

"s"+mid$(str$(i),2).FillColor=Color

I am unable to create the object name in this way.Can it be done? If so, how?

Answer:
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 = ColorNext i

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  Seven Service Boundary Mistakes That Create Technical Debt

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.