0

I'm sorry if a similar question is posted on here. I could not find this answer anywhere, but perhaps I could have if I knew which key words to search.

I am trying to name a variable within a for loop or find an alternative way to solve my problem. Here is an example of what I am trying to do.

for (int i = 0; i < 100; i++)
{
    string strName = "ImportedBody" + (i+1).ToString();
    IBody strName = model.GetEntity(strName) as IBody;
}

Essentially, my goal is to shorten:

IBody ImportedBody1 = model.GetEntity("ImportedBody1") as IBody;
IBody ImportedBody2 = model.GetEntity("ImportedBody2") as IBody;
IBody ImportedBody3 = model.GetEntity("ImportedBody3") as IBody;
IBody ImportedBody4 = model.GetEntity("ImportedBody4") as IBody;
IBody ImportedBody5 = model.GetEntity("ImportedBody5") as IBody;
IBody ImportedBody6 = model.GetEntity("ImportedBody6") as IBody;
...
IBody ImportedBody100 = model.GetEntity("ImportedBody100") as IBody;

into a for loop or something that makes that quicker to write.

The name could also change if that is a problem. For example:

IBody ImportedBody1a = model.GetEntity("ImportedBody1") as IBody;
IBody ImportedBody2a = model.GetEntity("ImportedBody2") as IBody;
IBody ImportedBody3a = model.GetEntity("ImportedBody3") as IBody;
IBody ImportedBody4a = model.GetEntity("ImportedBody4") as IBody;
IBody ImportedBody5a = model.GetEntity("ImportedBody5") as IBody;
IBody ImportedBody6a = model.GetEntity("ImportedBody6") as IBody;
...
IBody ImportedBody100a = model.GetEntity("ImportedBody100") as IBody;

Here are a few notes on what this program is supposed to do. The program I am writing is interacting with a CAD program. If I import geometry into CAD then I have a list of bodies named "ImportedBody1, ImportedBody2, ImportedBody3, etc.. In order to edit that body within my c# program I have to get that from the CAD program using:

IBody Name = model.GetEntity(StringNameAsLabeledWithinCAD) as IBody;

Now I can do things like change the direction that it is facing:

Name.SetNormalDirection(0, 1, 0);
2
  • 7
    This sounds like a job for a collection, not individual variables. Commented Jun 10, 2016 at 18:56
  • 8
    Just use an array or a List<IBody> instead. Why do you think you actually need 100 variables? Commented Jun 10, 2016 at 18:56

1 Answer 1

3

I am trying to name a variable within a for loop

The simple answer is... don't.

This is a job for collections (arrays, lists, etc.), not individual variables. Something perhaps as simple as this:

var importedBodies = new List<IBody>();
for (int i = 1; i <= 100; i++)
{
    var name = string.Format("ImportedBody{0}", i);
    var body = model.GetEntity(name) as IBody;

    importedBodies.Add(body);
}

After which you would have a collection called importedBodies which contains your 100 instances. You can directly reference a specific one with an index, such as:

importedBodies[10] // The 11th imported body

Or you can even query the data to find elements based on other criteria:

importedBodies.Where(b => b.SomeProperty = "some value") // or any other expressions to query/transform the values
Sign up to request clarification or add additional context in comments.

1 Comment

You're missing the token for string.Format("ImportedBody", i). I think you mean string.Format("ImportedBody{0}", i) :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.