2

i need to do something like this in c#. But in the Exec(object) i got a compilation error.

public class ParentClass { }
public class class1 : ParentClass
{
}
public class class2 : ParentClass
{
}
public class class3 : ParentClass
{
}

public class MasterClass 
{
    public void ExecutionMethod(ParentClass myObject)
    {
        //some code
        Exec(myObject);
        //some code
    }

    public void Exec(class1 obj)
    {
        //some code
    }
    public void Exec(class2 obj)
    {
        //some code
    }
    public void Exec(class3 obj)
    {
        //some code
    }
}

I solved using Reflection but i think must be a better approach, somebody could give me a nice idea

5
  • What compilation error did you get? Commented Apr 12, 2017 at 17:02
  • @EJoshuaS he is passing in a ParentClass but he has not overload that takes a ParentClass in. Commented Apr 12, 2017 at 17:03
  • @ScottChamberlain Yes, I did see that, just thought it was worth asking for the post to include the full error text (otherwise it's a lot less useful to future readers) Commented Apr 12, 2017 at 17:04
  • Have you heard of a command pattern? The classes you had Class1, Class2, Class3 will be concrete commands and MasterClass will be Invoker ( CommandManager), You can keep a list (Dictionary) each command and then the commandManager will have a logic to execute the right command Commented Apr 12, 2017 at 17:06
  • Yes, in this case i need to overload to takes a ParentClass. but i do not need that. I need a master method that could take any class that inherit from parentClass, and after pass the object to an specific method for an special behavior Commented Apr 12, 2017 at 17:07

5 Answers 5

2

As @ScottChamberlain pointed out in the comments, you don't have any methods that take an argument of type ParentClass.

Take a look at the Liskov substitution principle - if you've done your implementation properly, you can substitute an instance of, for example, class1 for an instance of ParentClass, but the converse is not true at all.

Odds are, you don't need (or want) the overloads anyway. Just have ParentClass be an abstract class with an abstract Execute method that all child classes have to implement, then you can just call Execute on the class directly without bothering with the overloads. Even better, just make ParentClass an interface. (This is sometimes called the Strategy Pattern by the way).

public interface IParent {
  void Execute();
}

public class class1 : ParentClass {
   //Execute method implementation
}

public class class2 : ParentClass {
   // ...
}

public class class3 : ParentClass {
  // ....
}

public class MasterClass 
{
    public void ExecutionMethod(IParent myObject)
    {
        //some code
        myObject.Execute();
        //some code
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

If I may, you might want to replace ParentClass with IParent in the declarations of class1, class2 and class3. Of course, I'm certain the OP has already figured that on their own...
1

I suggest you have a look at object-oriented design patterns. Specifically, the strategy pattern for this problem. Anyway, you can implement what you want like this:

public interface IParent 
{ 
    void Exec(); 
}
public class Child1 : IParent
{
    void Exec() { /*code*/ }
}
public class Child2 : IParent
{
    void Exec() { /*code*/ }
}
public class Child3 : IParent
{
    void Exec() { /*code*/ }
}

public class MasterClass 
{
    public void ExecutionMethod(IParent myObject)
    {
        //some code
        myObject.Exec();
        //some code
    }
}

You could also use an abstract class instead of an interface, if you wanted the parent class to have some functionality for the Exec method - then the child classes would have to override the method.

Comments

0

You can use command pattern, with dependency injection. I kind of give you an idea below. The concrete implementation will call execute on your receiver ( you logic goes there

public interface ICommand
{
    void Execute();
}

public class Command1 : ICommand
{
    public void Execute()
    {
        throw new NotImplementedException();
    }
}
public class Command2 : ICommand
{
    public void Execute()
    {
        throw new NotImplementedException();
    }
}
public class Command3 : ICommand
{
    public void Execute()
    {
        throw new NotImplementedException();
    }
}

public class CommandManager
{
    //you should use DI here to inject each concerete implementation of the command
    private Dictionary<string, ICommand> _commands;

    public CommandManager()
    {
        _commands = new Dictionary<string, ICommand>();
    }

    public void Execute(string key)
    {
        _commands[key].Execute();

    }
}

Comments

0

The error your seeing is a result of your class1,2,3 objects being cast as their parent type because of the signature of the ExecutionMethod(xxx). And not having an overridden method of Exec that takes a type of 'ParentClass' as the argument.

Probably the simplest method is to create an interface:

IDomainObject{}.
public class ParentClass : IDomainObject { }
public void ExecutionMethod(IDomainObject myObject)
    {        
        Exec(myObject);      
    }

Using the interface in this way will prevent the downcast during the method call.

Comments

0

You need to use an interface here

Try changing ParentClass like this:

public interface IParentClass{}

Then make each of your classes implement it, like this:

public class class1 : IParentClass
{
}

public class class2 : IParentClass
{
}

Then in your MasterClass, try this:

public void ExecutionMethod(IParentClass myObject)
{
    //some code
    Exec(myObject);
    //some code
}

public void Exec(IParentClass obj)
{
    //some code
}

And then you can pass in any of your classes that implement the IParentClassinterface.

Now as an enhancement - if you want every implementation of IParentClass to have some methods and properties that you can invoke in your Exec method, do it like so:

public interface IParentClass
{
    void DoTheThing();
}

This will force you to have this method in derived classes, so for example, class1 would look like this:

public class class1 : IParentClass
{
    public void DoTheThing() 
    {
        // things get done...
    }
}
public class class2 : IParentClass
{
    public void DoTheThing() 
    {
        // things get done a different way...
    }
}

And now in your Exec method, you can invoke like so:

public void Exec(IParentClass obj)
{
    obj.DoTheThing();
}

2 Comments

Thanks, but i need a specific behavior depending on the class. For example. If class1 then DoMethod1(class1.Name), if class2 then DoMethod2(class2.PhoneNumber); That is why i need a specific method foreach class
I've updated my answer to show how you can define methods for your derived classes. HTH

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.