Simple generics question

Simple generics question

Old forum URL: forums.lhotka.net/forums/t/7257.aspx


pondosinat posted on Friday, July 10, 2009

I need to pass in an argument that is of a base class type, eg: if I have the following two classes:

Dog : Animal(of Dog)

Animal(Of T as Animal(Of T)) : BusinessBase(Of T)

And I want declare a variable of type Animal (the base class), how would that look?

Thanks

 

 

skagen00 replied on Friday, July 10, 2009

Of the base class? You can't. Because Animal is a generic.

What I tend to do when I have generics in the hierarchy is give them an IAnimal interface. Applying IAnimal on the Animal<> class mean that anything that descends from Animal<> will be an IAnimal.

Then, you can have an IAnimal typed variable in Dog (if that's your goal). The members of IAnimal can be the various methods & properties declared/implemented in Animal<>.

pondosinat replied on Friday, July 10, 2009

Great tip, thanks!

Copyright (c) Marimer LLC