In a piece of code as below what is 'e'? I thought that in C# all variables have to be declared, so it isn't a variable. Is 'e' a member of the Element class ( a property) ? I don't understand what is is or how it knows what it is... I don't see it being declared ... have I missed the lesson about how to declare a new method?
foreach (Element e in new FilteredElementCollector(doc)
.OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Doors))
{
FamilyInstance fi = e as FamilyInstance;
FamilySymbol fs = fi.Symbol;
Family family = fs.Family;
info += family.Name + ": " + fs.Name + ": " + fi.Name + Environment.NewLine;
}
I appreciate that this is a beginner question, but I don't know where to look to fully understand this code.
And how is fi being declared, what type is it, and how does it know what type it is?
(this code uses the Autodesk Revit API)
Loading

VulpesPosted Mar 5, 2014, 4:32 PM
Duncan LithgowPosted Mar 5, 2014, 3:00 PM
Thanks for your answer. Thanks for the concept of control variables - useful term to remember.
I get that e is a variable, and when you say it is a variable of type Element that answers my confusion about the variables type not being declared. But 'Element' is not a type in C# - is it?
Variable types are
* string
* bool
* int
and so on.
How can Element be a type of variable?
VulpesPosted Mar 5, 2014, 5:07 AM
It is in fact the 'control variable' of the foreach statement and it only exists within the context of that statement.
You can also do this with a for statement:
for(int i = 0; i < 10; i++)
{
Console.WriteLine(i);
}
Here, 'i' is the control variable of type int of the for statement. Once the for statement ends 'i' is no longer defined i.e. its scope only extends over the for statement.
Any clearer?