0

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

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 1
    You seem to already know how to call a static method, such as `Core.DoSomethingElse();`, so what issue are you having? I don't understand the question. – gunr2171 Sep 06 '22 at 17:46
  • 3
    Fundamentally, you can't do what you're looking for. You'll need to pass *something* in to indicate which method you want to call. – Jon Skeet Sep 06 '22 at 17:48
  • @gunr2171 What I want to do is that the DoSomething() instruction into Core call the respective DoSomething() function in each class – LuisAlfredo92 Sep 06 '22 at 18:01
  • @JonSkeet Well, in this project it's easy since I have 2 classes only and I can do some validations with a switch – LuisAlfredo92 Sep 06 '22 at 18:07
  • 1
    static method inheritance is not supported in c# https://stackoverflow.com/questions/1380087/whats-the-correct-alternative-to-static-method-inheritance – BurnsBA Sep 06 '22 at 18:11
  • Is there a particular reason you're not using a parameterized class vs having two classes? – Krausladen Sep 06 '22 at 18:40

1 Answers1

2

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");
    }
}
Klaus Gütter
  • 11,151
  • 6
  • 31
  • 36
  • The first solution looks perfect to me, honestly I didn't think on use the interface *as parameter*, it will work great. Thanks a lot! And thanks for the delegate option, it looks more powerful if I want to add more classes – LuisAlfredo92 Sep 06 '22 at 19:14