troubleshooting Question

How to overload or another solution for same logic function in C#

Avatar of Skale
Skale asked on
C#
1 Comment1 Solution31 ViewsLast Modified:
Hello,

I'm using below code to get some information from my com object.

As you can see first and second functions content same only input is different one of them is IScrNamedObject and the other is IScrBody.

It's easy to overload but it's not sufficient i think to duplicate same logic.

Is there any solution for that?

        public static int GetObjectSubstrLevel(simpackcomslvLib.IScrNamedObject obj)
        {
            int level = 0;
            while (true)
            {
                switch (obj.parent.category)
                {
                    case "Model":
                        return level;
                    case "Variant":
                        return level;
                    case "Substr":
                        level += 1;
                        obj = obj.parent;
                        continue;
                    default:
                        obj = obj.parent;
                        continue;
                }
            }
        }

But  i have to declare this type of function so much for another input types like below;

        public static int GetObjectSubstrLevel(simpackcomslvLib.IScrBody obj)
        {
            int level = 0;
            while (true)
            {
                switch (obj.parent.category)
                {
                    case "Model":
                        return level;
                    case "Variant":
                        return level;
                    case "Substr":
                        level += 1;
                        obj = obj.parent;
                        continue;
                    default:
                        obj = obj.parent;
                        continue;
                }
            }
        }
ASKER CERTIFIED SOLUTION
Eduard Ghergu
Architect - Coder - Mentor

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros