RE: Difficulty Extending from Generic Base Class

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Your not missing anything apart from the diffculty in building generics to
work the way you want. BaseClass<Foo> does not inherit from BaseClass<T>,
its a different type.

Ciaran

"jkitagr" wrote:

I am trying to create a library that extends from a common base class that
provides common functionality, but I am running into one significant road
block. Let me give you an example.

If I do not do this with Generics, I would do something like this (Foo and
Bar extend from BaseClass):

public abstract class BaseClass
{
}

public class Foo : BaseClass
{
public override string ToString()
{
return "I am a Foo";
}
}

public class Bar : BaseClass
{
public override string ToString()
{
return "I am a Bar";
}
}

Then I could do some things like this:

Foo fooItem = new Foo();
Console.WriteLine(fooItem.ToString());

Bar barItem = new Bar();
Console.WriteLine(barItem.ToString());

And then add them to a common generic list like this:

List<BaseClass> listOfItems = new List<BaseClass>();
listOfItems.Add(fooItem);
listOfItems.Add(barItem);

foreach (BaseClass baseItem in listOfItems)
{
Console.WriteLine(baseItem.ToString());
}

The list of abstract base class items is a concrete class. But now I want
to make the base class generic, to have access to the type information like
this:

public abstract class BaseClass<T> where T : BaseClass<T>
{
public override string ToString()
{
return "I am a " + typeof(T).Name;
}
}

public class Foo : BaseClass<Foo>
{
}

public class Bar : BaseClass<Bar>
{
}

I can still do this, and it works:

Foo fooItem = new Foo();
Console.WriteLine(fooItem.ToString());

Bar barItem = new Bar();
Console.WriteLine(barItem.ToString());

But when I try to define a list of abstract base class items like:

List<BaseClass<>> listOfItems = new List<BaseClass<>>();

It expects a type parameter to be supplied. I can make it a list of
BaseClass<Foo> or BaseClass<Bar> but I want the list to be able to contain
either type. I could make the list of object, but then I don't have the
ability to restrict the type to Foo or Bar. I think what I am looking for is
a contraint that I can place on the type supplied to the list but there does
not seem to be any syntax for this.

Am I missing something in working with Generics?

Jim


.



Relevant Pages

  • Difficulty Extending from Generic Base Class
    ... Bar extend from BaseClass): ... public class Foo: BaseClass ... public override string ToString() ... The list of abstract base class items is a concrete class. ...
    (microsoft.public.dotnet.framework.clr)
  • Re: Generics and Casting
    ... > may be the main motivation behind generics). ... class Bar implements Foo { ...
    (comp.lang.java.programmer)
  • Re: generics and records
    ... Ada has a contract model of generics. ... >> Foo in the example above will never directly see the whole record, ... generic package specification. ... package Bar is ...
    (comp.lang.ada)
  • Re: Method chaining with generics
    ... The viewpoint of Oliver and John appears to be that a List is not ... screwed up generics from an OO perspective. ... example, given classes Foo and Bar, under an OO system is one can cast a Foo object to a Bar object, then it is safe to assume that any method on Bar is safely and correctly implemented in Foo. ...
    (comp.lang.java.programmer)
  • Re: Difficulty Extending from Generic Base Class
    ... Bar extend from BaseClass): ... public class Foo: BaseClass ... public override string ToString() ...
    (microsoft.public.dotnet.framework.clr)