[Not sure how else I would describe the subject, that pretty well sums it up]

Here's the set-up:

I have a C# project that references variables in another application:

TextFrame BodyCopyBox = (TextFrame)doc.FindElement("BodyCopyBox");

But what can happen is that I can have many of those like:

TextFrame BodyCopyBox1 = (TextFrame)doc.FindElement("BodyCopyBox1");
TextFrame BodyCopyBox2 = (TextFrame)doc.FindElement("BodyCopyBox2");
TextFrame BodyCopyBox3 = (TextFrame)doc.FindElement("BodyCopyBox3");
TextFrame BodyCopyBox4 = (TextFrame)doc.FindElement("BodyCopyBox4");

Once I have each element, I can do things like (and this is a small example of features):

BodyCopyBox1.Height = dHeightToDraw.ToString() + "in";
BodyCopyBox1.Width = dWidthToDraw.ToString() + "in";

So... here's the question. Is there a way that I can set up the variable names to include a variable? Something like:

for (int i=0; i<100; i++)
{
      BodyCopyBox[i].Height = "8in";
}

Recommended Answers

All 2 Replies

Yes, but instead of using variables you should use an array. Try this code:

int numberOfBoxes = 10;
TextFrame[] BodyCopyBoxes = new TextFrame[numberOfBoxes];

BodyCopyBoxes[0] = (TextFrame)doc.FindElement("BodyCopyBox");
BodyCopyBoxes[1] = (TextFrame)doc.FindElement("BodyCopyBox1");
BodyCopyBoxes[2] = (TextFrame)doc.FindElement("BodyCopyBox2");
BodyCopyBoxes[3] = (TextFrame)doc.FindElement("BodyCopyBox3");
BodyCopyBoxes[4] = (TextFrame)doc.FindElement("BodyCopyBox4");

// Loop through all the boxes

for (int i = 0; i < BodyCopyBoxes.Length; i++)
{
   BodyCopyBoxes[i].Height = "8in";
}

Thanks

Perfect. So simple, and yet it escaped me. Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.