Generics - How do you do this in one class?

D

D. Yates

How do you do this with one class instead of two?

This works:

public class UlMenu1<T> where T : UlMenuItem
{

}

// I want UlMenu2 to implement IComparable (UlBeverageItem already does,
but is not shown here).
public class UlMenu2 : UlTest1<UlBeverageItem>, IComparable
{
#region IComparable Members
public int CompareTo(object obj)
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
}


This does not mean the same thing:
public class UlMenu1<T> where T : UlMenuItem, IComparable
{
}



Also, how do you have a generic input item that must be a class and still
inherit from another class?
public class UlMenu1<T> where T : UlMenuItem :
UlBaseMenuClassForUlMenu1ToInheritFrom
{

}


Thanks,
Dave
 
M

Mattias Sjögren

Dave,
How do you do this with one class instead of two?

This works:

public class UlMenu1<T> where T : UlMenuItem
{

}

// I want UlMenu2 to implement IComparable (UlBeverageItem already does,
but is not shown here).
public class UlMenu2 : UlTest1<UlBeverageItem>, IComparable
{
#region IComparable Members
public int CompareTo(object obj)
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
}


This does not mean the same thing:
public class UlMenu1<T> where T : UlMenuItem, IComparable
{
}


Not sure I understand the problem here. Are you looking for this?

public class UlMenu1<T> : IComparable where T : UlMenuItem,
{
}

Also, how do you have a generic input item that must be a class and still
inherit from another class?
public class UlMenu1<T> where T : UlMenuItem :
UlBaseMenuClassForUlMenu1ToInheritFrom
{

}


public class UlMenu1<T> : UlBaseMenuClassForUlMenu1ToInheritFrom where
T : UlMenuItem


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top