Avatar of Skale
Skale

asked on 

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

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;
                }
            }
        }

Open in new window


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;
                }
            }
        }

Open in new window

C#

Avatar of undefined
Last Comment
Eduard Ghergu
ASKER CERTIFIED SOLUTION
Avatar of Eduard Ghergu
Eduard Ghergu
Flag of Romania image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo