Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. inheriting from a class and also using a constraint

inheriting from a class and also using a constraint

Scheduled Pinned Locked Moved C#
tutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bwilhite
    wrote on last edited by
    #1

    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

    P T 2 Replies Last reply
    0
    • B bwilhite

      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

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      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.

      My blog | My articles

      B 1 Reply Last reply
      0
      • P Pete OHanlon

        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.

        My blog | My articles

        B Offline
        B Offline
        bwilhite
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • B bwilhite

          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

          T Offline
          T Offline
          Tim Paaschen
          wrote on last edited by
          #4

          This one compiles (in VS2005): class BarStream<T> : IBarStream where T : Barable { } Regards, Tim

          B 1 Reply Last reply
          0
          • T Tim Paaschen

            This one compiles (in VS2005): class BarStream<T> : IBarStream where T : Barable { } Regards, Tim

            B Offline
            B Offline
            bwilhite
            wrote on last edited by
            #5

            I believe that's it! I had an extra comma in there, and I think that's what was throwing it. Now I've got other problems to deal with :) Thanks for the help. BW

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups