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. inheritance problem

inheritance problem

Scheduled Pinned Locked Moved C#
helpalgorithmsoopquestion
5 Posts 2 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.
  • S Offline
    S Offline
    steven shingler
    wrote on last edited by
    #1

    Hi all, I'm trying to make a class (DSWrapper) which extends DataSet, so I can put in a customized Sort() method: class DSWrapper : DataSet { public DataSet Sort() { //sorting code } } To call it I have another class (EditRecords) with a method (ListAll) that returns a DataSet, thus: DSWrapper dsw = (DSWrapper) EditRecords.ListAll(); dsw.Sort(); //...then bind to datagrid However I get a runtime exception because the cast is not valid. (Dataset -> DSWrapper) This makes sense I suppose, but is there a way round it? Is my idea just damn silly? :) If anyone could help that would be really great. Best Wishes Steven

    M 1 Reply Last reply
    0
    • S steven shingler

      Hi all, I'm trying to make a class (DSWrapper) which extends DataSet, so I can put in a customized Sort() method: class DSWrapper : DataSet { public DataSet Sort() { //sorting code } } To call it I have another class (EditRecords) with a method (ListAll) that returns a DataSet, thus: DSWrapper dsw = (DSWrapper) EditRecords.ListAll(); dsw.Sort(); //...then bind to datagrid However I get a runtime exception because the cast is not valid. (Dataset -> DSWrapper) This makes sense I suppose, but is there a way round it? Is my idea just damn silly? :) If anyone could help that would be really great. Best Wishes Steven

      M Offline
      M Offline
      mav northwind
      wrote on last edited by
      #2

      Hi Steven! You could add a constructor to your DSWrapper taking a DataSet as parameter:

      public DSWrapper(DataSet theDataSet)
      {
      // store the DataSet reference somewhere
      }

      and then perform

      DSWrapper dsw = new DSWrapper(EditRecords.ListAll());
      dsw.Sort();

      mav

      S 1 Reply Last reply
      0
      • M mav northwind

        Hi Steven! You could add a constructor to your DSWrapper taking a DataSet as parameter:

        public DSWrapper(DataSet theDataSet)
        {
        // store the DataSet reference somewhere
        }

        and then perform

        DSWrapper dsw = new DSWrapper(EditRecords.ListAll());
        dsw.Sort();

        mav

        S Offline
        S Offline
        steven shingler
        wrote on last edited by
        #3

        Hi mav, thanks for replying. Yes that works, but it means the inheritance part of it is "by the by" doesn't it? That's a "has a" rather than "is a" class. Is there a way to do it with inheritance? Steven

        M 1 Reply Last reply
        0
        • S steven shingler

          Hi mav, thanks for replying. Yes that works, but it means the inheritance part of it is "by the by" doesn't it? That's a "has a" rather than "is a" class. Is there a way to do it with inheritance? Steven

          M Offline
          M Offline
          mav northwind
          wrote on last edited by
          #4

          You could also alter your EditRecords class to return a DSWrapper object instead of a DataSet, but honestly I wouldn't. You could, however, leave the interface of EdtRecords.ListAll untouched and just return a DSWrapper object. DSWrapper inherits DataSet, so this should work. Then you _could_ downcast to DSWrapper and call Sort, but that's not very obvious from a design point of view. I think it's much cleaner to instantiate a subclass. Your DSWrapper class adds new functionality to DataSet, so this functionality only can be used if the object itself _is_ an instance of DSWrapper. No way to alter DataSet itself to add a Sort method. Regards, mav

          S 1 Reply Last reply
          0
          • M mav northwind

            You could also alter your EditRecords class to return a DSWrapper object instead of a DataSet, but honestly I wouldn't. You could, however, leave the interface of EdtRecords.ListAll untouched and just return a DSWrapper object. DSWrapper inherits DataSet, so this should work. Then you _could_ downcast to DSWrapper and call Sort, but that's not very obvious from a design point of view. I think it's much cleaner to instantiate a subclass. Your DSWrapper class adds new functionality to DataSet, so this functionality only can be used if the object itself _is_ an instance of DSWrapper. No way to alter DataSet itself to add a Sort method. Regards, mav

            S Offline
            S Offline
            steven shingler
            wrote on last edited by
            #5

            Thanks again mav I agree - it wasn't a good idea. I'll stick with the standalone class using a DataSet instance, rather than inheriting. Cheers Steven

            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