By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,214 Members | 1,496 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,214 IT Pros & Developers. It's quick & easy.

Covariance question

P: n/a
Hi. I want the parameter type of a function to be the same type of the class.
One solution would be:
class Animal<TAnimal> where TAniman : Animal
{
public virtual void DoSomethingToTheAnimal(TAnimal animal)
{
////
}
}

class Dog : Animal<TAniman> where TAnimal : Dog
{
}
But I prefer not to require the user to include a statement like

where TAnimal : Dog

every time a class is inherited. Is there a better way to do this?

Thanks
Matt Brown
May 20 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
PDHB <PD**@discussions.microsoft.com> wrote:

I'm not sure if your question is asking about generics or inheritance.
Hi. I want the parameter type of a function to be the same type of the class.
You want the parameter type of a method to be the same type as what
class, exactly? The descendant class? The generic type parameter's
class?
One solution would be:
class Animal<TAnimal> where TAniman : Animal
{
public virtual void DoSomethingToTheAnimal(TAnimal animal)
{
////
}
}

class Dog : Animal<TAniman> where TAnimal : Dog
{
}
I can't make out what you're trying to show. Are you missing definitions
for Animal and Dog? Is this second definition of Dog meant to be generic
(i.e. be Dog<>, not Dog)? Is it about the DoSomethingToTheAnimal() being
overridden?
But I prefer not to require the user to include a statement like

where TAnimal : Dog

every time a class is inherited. Is there a better way to do this?


I have no idea what you're trying to do.

-- Barry

--
http://barrkel.blogspot.com/
May 20 '06 #2

P: n/a
"PDHB" <PD**@discussions.microsoft.com> a écrit dans le message de news:
9A**********************************@microsoft.com...

| Hi. I want the parameter type of a function to be the same type of the
class.
| One solution would be:
| class Animal<TAnimal> where TAniman : Animal
| {
| public virtual void DoSomethingToTheAnimal(TAnimal animal)
| {
| ////
| }
| }
|
| class Dog : Animal<TAniman> where TAnimal : Dog
| {
| }

class Animal<TAnimal> where TAnimal : Animal
{
public virtual void DoSomethingToTheAnimal(TAnimal animal)
{
////
}
}

class Dog : Animal<Dog>
{
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
May 20 '06 #3

P: n/a
"Joanna Carter [TeamB]" <jo****@not.for.spam> a écrit dans le message de
news: %2**********************@TK2MSFTNGP03.phx.gbl...

Sorry, I answered before checking this with the compiler :-)

| class Animal<TAnimal> where TAnimal : Animal

This line won't compile unless you also declare a non-generic Animal class.

The concept of an Animal that "works with" an Animal seems somewhet wrong.

If you intend to derive Dog from Animal, then you would not need to use a
generic base type.

I think you might need to do something like this :

class Animal
{
}

class Animal<animalT> : Animal where animalT : Animal
{
public virtual void DoSomethingToTheAnimal(animalT animal)
{
////
}
}

class Dog : Animal<Dog>
{
}

But it still doesn't seem like a good design to me.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
May 20 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.