Home > OS >  How to call functions with the same name in different classes from a common one?
How to call functions with the same name in different classes from a common one?

Time:09-07

I have 2 different classes with a function with the same name:

class Class1
{
    public static void Main()
    {
        Core.DoSomethingElse();
    }

    private static void DoSometing(string someParam)
    {
        Console.WriteLine("Hello from class 1");
    }
}


class Class2
{
    public static void Main()
    {
        Core.DoSomethingElse();
    }

    private static void DoSometing(string someParam)
    {
        Console.WriteLine("Hello from class 2");
    }
}

And a Core class

What I want to do is to call these DoSomething() methods from the Core, since Class1 and Class2 will use Core:

Class Core
{
    public static void DoSomethingElse(string someParam)
    {
        // Some code

        /* This is where I want to call the method and
         * **depending on which class is executing DoSomethingElse() method**
         * call **its own DoSomething()** method
         */
        DoSometing();
        Console.WriteLine("Hello from Core");
        //More code
    }
}

Then I want to do something like this: If executing Class1, output:

Hello from class 1
Hello from Core

If executing Class2, output:

Hello from class 2
Hello from Core

I tried using interfaces but, since Class1 and Class2 must be static it didn't work, I also tried using virtual and abstract methods with inheritance, but then again it didn't work since methods must be static

CodePudding user response:

Here is a solution using a common interface.

Move from static to instance methods (you said you cannot do this, but why?).

Then have both classes implement a common interface (or base class). Finally, pass the interface reference to the common method so it can call back.

interface ICommon
{
    void DoSometing(string someParam);
}

class Class1 : ICommon
{
    public static void Main()
    {
        Core.DoSomethingElse(new Class1(), "param1");
    }

    public void DoSometing(string someParam)
    {
        Console.WriteLine("Hello from class 1");
    }
}


class Class2 : ICommon
{
    public static void Main()
    {
        Core.DoSomethingElse(new Class2(), "param2");
    }

    public void DoSometing(string someParam)
    {
        Console.WriteLine("Hello from class 2");
    }
}


class Core
{
    public static void DoSomethingElse(ICommon common, string someParam)
    {
        common.DoSometing(someParam);
        Console.WriteLine("Hello from Core");
    }
}

If you really have to use static methods, you might pass a delegate to DoSomethingElse instead:

class Class1
{
    public static void Main()
    {
        Core.DoSomethingElse(DoSometing, "param1");
    }

    private static void DoSometing(string someParam)
    {
        Console.WriteLine("Hello from class 1");
    }
}


class Class2
{
    public static void Main()
    {
        Core.DoSomethingElse(DoSometing, "param2");
    }

    private static void DoSometing(string someParam)
    {
        Console.WriteLine("Hello from class 2");
    }
}


class Core
{
    public static void DoSomethingElse(Action<string> action, string someParam)
    {
        action(someParam);
        Console.WriteLine("Hello from Core");
    }
}
  • Related