© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
11 replies
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:
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);
            }
        }
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

✅ If statement.
C#CC# / help
4y ago
if statements with more than once condition
C#CC# / help
2y ago