3

I wont to use only one method to unbox like this:

public interface IModule<T, U> where T : BaseBox where U : BaseItem { U[] GetItems<T>( int id ); } public sealed partial class Module<T, U> : IModule<T, U> where T : BaseBox where U : BaseItem { U[] IModule<T, U>.GetItems<T>( int id ) { return T.Unboxing(); // It is wrong! } } 

But I can't. How do I have to write right generics?

Next code to understand.I have item's types:

public abstract class BaseItem { protected int _id; protected string _description; } public sealed class TriangleItem : BaseItem { public int TriangleId { get { return _id; } set { _id = value; } } public string TriangleDescription { get { return _description; } set { _description = value; } } public Color color { get; set; } } public sealed class CircleItem : BaseItem { public int CircleId { get { return _id; } set { _id = value; } } public string CircleDescription { get { return _description; } set { _description = value; } } public int Radius { get; set; } } 

Then I have boxes for items:

public abstract class BaseBox { public string ItemsXml { get; set; } public abstract BaseItem[] Unboxing(); } public sealed class TriangleBox : BaseBox { public TriangleItem[] Unboxing() { return Util.FromXml( ItemsXml ).Select( i => new TriangleItem { TriangleId = int.Parse( i ), TriangleDescription = i, Color = Color.Red } ).ToArray(); } } public sealed class CircleBox : BaseBox { public CircleItem[] Unboxing() { return Util.FromXml( ItemsXml ).Select( i => new CircleItem { CircleId = int.Parse( i ), CircleDescription = i, Radius = 5 } ).ToArray(); } } 

Here I have different implementations Unboxing-method.

3
  • 2
    You have written T.Unboxing() as though it is a static method, you need an instance to use Commented Aug 23, 2013 at 7:30
  • You'll either need to ask for the correct T as argument, or use your int id to get it. Commented Aug 23, 2013 at 7:33
  • I really think you should drop generics and try to do pure OOP design. Generics in C# are not as powerful as other generic-based languages, like Haskell. Abusing generics like that is only asking for problems. Commented Aug 23, 2013 at 7:46

2 Answers 2

3

As Sayse mentioned in the comment, you are trying to use T as a static method and need an instance. For example,

public sealed partial class Module<T, U> : IModule<T, U> where T : BaseBox where U : BaseItem { private T _box; public Module(T box) { _box = box; } U[] IModule<T, U>.GetItems<T>( int id ) { // You need to determine how id relates to _box. return _box.Unboxing(); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Cheers, didn't have time to completely write and check an answer :)
0

First of all coming to your interface definition:

public interface IModule<T, U> where T : BaseBox where U : BaseItem { U[] GetItems<T>( int id ); } 

You can simply declare GetItems<T>(int id) as GetItems(int id).

Your code return T.Unboxing() is wrong because T represents a type (For e.g. the classes TriangleBox, CircleBox etc.) and not the object you want to call your method on. You can fix this either by taking the particular object as a parameter in GetItems or by taking the BaseBox T as a constructor argument. i.e. either

U[] IModule<T, U>.GetItems(T box, int id ) { return box.Unboxing(); // I don't know what you plan to do with id } 

or

private readonly T _box; public Module(T box) { _box = box; } U[] IModule<T, U>.GetItems(int id ) { return _box.Unboxing(); // I don't know what you plan to do with id } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.