inheriting from a class and also using a constraint
-
I have an interface: public interface IBarStream {} an abstract class: public abstract class Barable {} And I'm trying to create a generic class that inherits from Barable, but also constrains the Type passed in to being Barable: so I have public class BarStream where T: Barable {} ...but how to specify that BarStream inherits from IBarStream? Any ideas? Here's what doesn't work: public class BarStream where T : Barable : IBarStream {} ...doesn't work public class BarStream : IBarStream, where T : Barable {} ...doesn't work either I think if I made IBarStream generic, i.e. IBarStream, then I could get it to work, but I have no reason for IBarStream to be generic here... Can this be done? BW
-
I have an interface: public interface IBarStream {} an abstract class: public abstract class Barable {} And I'm trying to create a generic class that inherits from Barable, but also constrains the Type passed in to being Barable: so I have public class BarStream where T: Barable {} ...but how to specify that BarStream inherits from IBarStream? Any ideas? Here's what doesn't work: public class BarStream where T : Barable : IBarStream {} ...doesn't work public class BarStream : IBarStream, where T : Barable {} ...doesn't work either I think if I made IBarStream generic, i.e. IBarStream, then I could get it to work, but I have no reason for IBarStream to be generic here... Can this be done? BW
Off the top of my head you need to do this:
public interface IBarStream{} public abstract class Barable : IBarStream {} public class Barstream<T> : Barable where T : Barable, new() {} // The new constraint tells you that you can do a new on a variable of type T
Deja View - the feeling that you've seen this post before.
-
Off the top of my head you need to do this:
public interface IBarStream{} public abstract class Barable : IBarStream {} public class Barstream<T> : Barable where T : Barable, new() {} // The new constraint tells you that you can do a new on a variable of type T
Deja View - the feeling that you've seen this post before.
That would work, but doesn't do what I need. My generic class implements IBarStream AND the generic parameter (T) it uses has to be Barable..it's combining these two things that I can't seem to get down. One thing I could do is remove the IBarStream interface, make the BarStream class abstract, and then take the IBarStream members and put them in the BarStream as abstract...(I hope I communicated that well enough). But, I would think there should be a way to do it this first way I have it set up (with the interfaces). BW
-
I have an interface: public interface IBarStream {} an abstract class: public abstract class Barable {} And I'm trying to create a generic class that inherits from Barable, but also constrains the Type passed in to being Barable: so I have public class BarStream where T: Barable {} ...but how to specify that BarStream inherits from IBarStream? Any ideas? Here's what doesn't work: public class BarStream where T : Barable : IBarStream {} ...doesn't work public class BarStream : IBarStream, where T : Barable {} ...doesn't work either I think if I made IBarStream generic, i.e. IBarStream, then I could get it to work, but I have no reason for IBarStream to be generic here... Can this be done? BW
This one compiles (in VS2005): class BarStream<T> : IBarStream where T : Barable { } Regards, Tim
-
This one compiles (in VS2005): class BarStream<T> : IBarStream where T : Barable { } Regards, Tim