1

stoopid question time again.

I have this class that pulls in some code via a base class like so:

class TVIRoot : OURTreeNodeImpl { }

I now want to add some template functionality

class TVIRoot<TLabelHandler> : OURTreeNodeImpl { }

But I can't work out what sort of finger mangling I need to get it to compile when I need to supply some constraints.

class TVIRoot<TLabelHandler> where TLabelHandler : new(), OURTreeNodeImpl { } //no    
class TVIRoot<TLabelHandler> where TLabelHandler : SomeClass : OURTreeNodeImpl { } //no
class TVIRoot<TLabelHandler> : OURTreeNodeImpl, where TLabelHandler : SomeClass { } //no

Can this be done?

Many thanks.

bg

Davin Tryon
  • 66,517
  • 15
  • 143
  • 132
push 22
  • 1,172
  • 3
  • 15
  • 34

2 Answers2

2
class TVIRoot<TLabelHandler> : OURTreeNodeImpl where TLabelHandler : SomeClass { } //yes
Henrik
  • 23,186
  • 6
  • 42
  • 92
0

The constraint comes after the base class inheritance, here is an example:

public interface  IFood
{
}

public class Animal
{
}

public class Cat<T> : Animal where T : IFood
{
    public void Eat(T food)
    {
    }
}

for more details check: http://msdn.microsoft.com/en-US/library/d5x73970(v=vs.80).aspx

Siraf
  • 1,133
  • 9
  • 24