0

Is there a way to automatically cast an object to a specific type when passing that object into an overloaded method?

I have three classes that all inherit from a base class

class Cat : Animal
class Dog : Animal
class Tiger : Animal

I have another class, a class that writes to a database (dbInterface) that has an overloaded create method for each of these types

void Create(Cat cat);
void Create(Dog dog);
void Create(Tiger tiger);

I would like to call a Create method like so

Animal cat = new Cat();

dbInterface.Create(cat);

and I'd like this to call the Create(Cat cat) method specifically. Currently this doesn't happen because cat is type Animal.

Currently what I'm doing that I don't particularly like is I have a generic create method Create(Animal animal) and in that method I check for a valid cast and call the appropriate method.

void Create(Animal animal)
{
    Cat cat = animal as Cat;
    if (cat != null)
    {
         Create(cat);
    }
... for Dog and Tiger too ...
}

Is there a better way to do this or am I doing something stupid?

RBarnes
  • 3
  • 2

2 Answers2

2

The concept/language feature you're looking for is called "Dynamic/Double Dispatch". For languages that don't have it, there is a technique called "The Visitor Pattern".

That should be enough to get you started, but let me find some resources I've used in the past...

Oh, wow, it turns out C# has this feature already when you cast the argument!

Kache
  • 15,647
  • 12
  • 51
  • 79
-1

unless i'm missing something, why are you overloading Create() instead of:

void Create(Animal animal)

this way any class that implements Animal can be passed as an argument

Animal cat = new Cat();
dbInterface.Create(cat);
SAR
  • 180
  • 1
  • 9
  • Not sure how this is different from "Currently what I'm doing that I don't particularly like is I have a generic create method `Create(Animal animal)` and in that method I check for a valid cast and call the appropriate method." stated in the post... Some clarification may help - so far it does not look like answer. – Alexei Levenkov Mar 05 '19 at 22:05
  • But you don't need to cast anything. The method Create takes any object that implements Animal. All the casting your' doing is unnecessary – SAR Mar 05 '19 at 22:12
  • Please show implementation for `Create(Animal)` that does what OP wants - in particular calls 3 different versions of `Create` - so it is very clear how you propose to solve double dispatch. – Alexei Levenkov Mar 05 '19 at 22:32
  • tbh, I don't understand what you're trying to do. `void Create(Animal animal)` in db class is all you need, and then call this method with any object as arg. it can't be simpler than that – SAR Mar 05 '19 at 22:53
  • What I was trying to do is to figure out why you believe repeating what OP said in the question is the answer. Since I think there is a good existing answer (which I used as duplicate) I don't see much reason to continue that effort on my side. – Alexei Levenkov Mar 06 '19 at 18:43