C
C#2y ago
Curious

❔ different variable type depending on condition of IF statement

Hi there, how would I go about having a different type on a variable with the same name, for later use? This is required for use in my code later on within the Autodesk RevitAPI
var elmFilter = null;
if (FilterSelected == 0)
{
Helpers.ElementFilterTerminals elmFilter = new Helpers.ElementFilterTerminals();
}
if (FilterSelected == 1)
{
Helpers.ElementFilterDuctwork elmFilter = new Helpers.ElementFilterDuctwork();
}

//later on...

pickedRef = sel.PickObject(ObjectType.PointOnElement, elmFilter, "Pick the elements to be updated.");
var elmFilter = null;
if (FilterSelected == 0)
{
Helpers.ElementFilterTerminals elmFilter = new Helpers.ElementFilterTerminals();
}
if (FilterSelected == 1)
{
Helpers.ElementFilterDuctwork elmFilter = new Helpers.ElementFilterDuctwork();
}

//later on...

pickedRef = sel.PickObject(ObjectType.PointOnElement, elmFilter, "Pick the elements to be updated.");
in my helpers class:
public class ElementFilterTerminals : ISelectionFilter
{
public bool AllowElement(Element e)
{
//TO-DO: Add Magicad Family for end caps to this.
return (e.Category.Id.IntegerValue.Equals(
(int)BuiltInCategory.OST_DuctTerminal)) ||
(e.Category.Id.IntegerValue.Equals(
(int)BuiltInCategory.OST_MechanicalEquipment));
}
public bool AllowReference(Autodesk.Revit.DB.Reference r, XYZ p)
{

return (r.ElementReferenceType == ElementReferenceType.REFERENCE_TYPE_SURFACE);
}
}

public class ElementFilterDuctwork : ISelectionFilter
{
public bool AllowElement(Element e)
{
return (e.Category.Id.IntegerValue.Equals(
(int)BuiltInCategory.OST_DuctSystem)) ||
(e.Category.Id.IntegerValue.Equals(
(int)BuiltInCategory.OST_DuctFitting));
}
public bool AllowReference(Autodesk.Revit.DB.Reference r, XYZ p)
{

return (r.ElementReferenceType == ElementReferenceType.REFERENCE_TYPE_SURFACE);
}
}
public class ElementFilterTerminals : ISelectionFilter
{
public bool AllowElement(Element e)
{
//TO-DO: Add Magicad Family for end caps to this.
return (e.Category.Id.IntegerValue.Equals(
(int)BuiltInCategory.OST_DuctTerminal)) ||
(e.Category.Id.IntegerValue.Equals(
(int)BuiltInCategory.OST_MechanicalEquipment));
}
public bool AllowReference(Autodesk.Revit.DB.Reference r, XYZ p)
{

return (r.ElementReferenceType == ElementReferenceType.REFERENCE_TYPE_SURFACE);
}
}

public class ElementFilterDuctwork : ISelectionFilter
{
public bool AllowElement(Element e)
{
return (e.Category.Id.IntegerValue.Equals(
(int)BuiltInCategory.OST_DuctSystem)) ||
(e.Category.Id.IntegerValue.Equals(
(int)BuiltInCategory.OST_DuctFitting));
}
public bool AllowReference(Autodesk.Revit.DB.Reference r, XYZ p)
{

return (r.ElementReferenceType == ElementReferenceType.REFERENCE_TYPE_SURFACE);
}
}
7 Replies
Anton
Anton2y ago
use the common type, ISelectionFilter if selection works with that interface, it will work if it works via overloads, you can't
Curious
CuriousOP2y ago
Curious
CuriousOP2y ago
Curious
CuriousOP2y ago
Unless I misunderstood what you meant for me to do..?
Anton
Anton2y ago
kinda assign to the variable above you're declaring new ones with the same name
Curious
CuriousOP2y ago
Ah Yes I see, perfect thank you!
ISelectionFilter elmFilter = null;
if (FilterSelected == 0)
{
elmFilter = new Helpers.ElementFilterTerminals();
}
if (FilterSelected == 1)
{
elmFilter = new Helpers.ElementFilterDuctwork();
}
ISelectionFilter elmFilter = null;
if (FilterSelected == 0)
{
elmFilter = new Helpers.ElementFilterTerminals();
}
if (FilterSelected == 1)
{
elmFilter = new Helpers.ElementFilterDuctwork();
}
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server