Creating a constrained class that implements an interface (C#2.0)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have this class:

public class ItemType
....

public class ProductType<T> where T: ItemType
....

Now I want to add an IDisposable interface to ProductType...
 
public class ItemType
{
....
}

public class ProductType : ItemType, IDisposable
{
....


public void Dispose()
{
// Clean-up stuff here
}

}
 
Thanks for the response, but that changes the implementation.

ProductType is not a type of "ItemType" but a container for "ItemType's",
like List<T> but the "T" needs to be constrained to a specific type.

Example: I want to build a car, so I use car items, but I want to prevent
anyone from using bicycle items.
 
Sorry I misread your code. I thought ProductType was an ItemType. I'm
still confused about the <T> syntax you have at the top there. Oh crap - is
this VB? I'm in C# mode, have never used VB.NET (used 6 tho).

In any case, just implement IDisposable and provide a Dispose() method. So
in VB.NET you'd just put 'Implements IDisposable' right after your class
declaration. The designer will stub out the Dispose() method for you.
 
Leicester B. Ford Jr. said:
I have this class:

public class ItemType
...

public class ProductType<T> where T: ItemType
...

public class ProductType<T> : IDisposable, <whatever else you want to
inherit\implement>
where T : ITemType

//...
 
I think my brain is fried - been a long day. Now I see that this is C#,
not VB -should have read the title of the message. Further, I'm not
familiar with generics so I was confused. Don't you implement an interface
the same was as in C# 1.0 - like:

public class ProductType<T> : IDisposable

???
 
Wow, that works... looks wierd though. I thought I had tried it before, but
I guess I didn't get it just right.

I just have to make sure that the constraint is the last thing in the list.
 
Back
Top