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. Abstracts: can I do better than this?

Abstracts: can I do better than this?

Scheduled Pinned Locked Moved C#
question
18 Posts 9 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.
  • M MollyTheCoder

    I know I'm missing something simple. I have a base class, Device, and a bunch of derived classes Dev1, Dev2... Depending on the type of device active at the moment, I have an abstract method for each derived type. Deciding which to use leads me to:

    if (SelectedDevice.GetType() == typeof(Dev1))
    {
    SetDevice((Dev1)SelectedDevice);
    }
    else if (SelectedDevice.GetType() == typeof(Dev2))
    {
    SetDevice((Dev2)SelectedDevice);
    }
    else if ...

    It works, but it just feels ugly. Molly

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #7

    MollyTheCoder wrote:

    I have a base class, Device, and a bunch of derived classes Dev1, Dev2

    SetDevice((Device)SelectedDevice);

    This one?

    I are Troll :suss:

    1 Reply Last reply
    0
    • M MollyTheCoder

      I know I'm missing something simple. I have a base class, Device, and a bunch of derived classes Dev1, Dev2... Depending on the type of device active at the moment, I have an abstract method for each derived type. Deciding which to use leads me to:

      if (SelectedDevice.GetType() == typeof(Dev1))
      {
      SetDevice((Dev1)SelectedDevice);
      }
      else if (SelectedDevice.GetType() == typeof(Dev2))
      {
      SetDevice((Dev2)SelectedDevice);
      }
      else if ...

      It works, but it just feels ugly. Molly

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #8

      Is the casting required at all? Even if SetDevice is overloaded for each type there shouldn't be a problem should there? And if not, then you still don't need to cast to each type. Edit: I just tried it and I see the problem. More information about SetDevice would be helpful. And, as Luc said, use is rather than comparing types. Edit: Or perhaps SetDevice should handle determining which type of device it was passed:

      private static void
      SetDev
      (
      DevBase Dev
      )
      {
      System.Console.WriteLine ( "DevBase" ) ;

      if ( Dev is Dev1 ) SetDev ( (Dev1) Dev ) ;
      else if ( Dev is Dev2 ) SetDev ( (Dev2) Dev ) ;
      
      return ;
      

      }

      modified on Wednesday, December 30, 2009 2:01 PM

      M 2 Replies Last reply
      0
      • P PIEBALDconsult

        Is the casting required at all? Even if SetDevice is overloaded for each type there shouldn't be a problem should there? And if not, then you still don't need to cast to each type. Edit: I just tried it and I see the problem. More information about SetDevice would be helpful. And, as Luc said, use is rather than comparing types. Edit: Or perhaps SetDevice should handle determining which type of device it was passed:

        private static void
        SetDev
        (
        DevBase Dev
        )
        {
        System.Console.WriteLine ( "DevBase" ) ;

        if ( Dev is Dev1 ) SetDev ( (Dev1) Dev ) ;
        else if ( Dev is Dev2 ) SetDev ( (Dev2) Dev ) ;
        
        return ;
        

        }

        modified on Wednesday, December 30, 2009 2:01 PM

        M Offline
        M Offline
        MollyTheCoder
        wrote on last edited by
        #9

        I have a form with device information for for each type of device. Most of the information is similar, so I'm sharing a single form, using SetDevice() to setup fields and controls specific to each derived device type. (right now, all SetDevice methods are inside the form, not the Device classes). SelectedDevice is instantiated as a Device, then cast to whichever derived class is appropriate. The cast is necessary because at compile-time, SelectedDevice is just a Device, but at run-time will have a specific derived type.

        P 1 Reply Last reply
        0
        • M MollyTheCoder

          I have a form with device information for for each type of device. Most of the information is similar, so I'm sharing a single form, using SetDevice() to setup fields and controls specific to each derived device type. (right now, all SetDevice methods are inside the form, not the Device classes). SelectedDevice is instantiated as a Device, then cast to whichever derived class is appropriate. The cast is necessary because at compile-time, SelectedDevice is just a Device, but at run-time will have a specific derived type.

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #10

          I wonder whether or not dynamics in C# 4 will do it.

          1 Reply Last reply
          0
          • P PIEBALDconsult

            Is the casting required at all? Even if SetDevice is overloaded for each type there shouldn't be a problem should there? And if not, then you still don't need to cast to each type. Edit: I just tried it and I see the problem. More information about SetDevice would be helpful. And, as Luc said, use is rather than comparing types. Edit: Or perhaps SetDevice should handle determining which type of device it was passed:

            private static void
            SetDev
            (
            DevBase Dev
            )
            {
            System.Console.WriteLine ( "DevBase" ) ;

            if ( Dev is Dev1 ) SetDev ( (Dev1) Dev ) ;
            else if ( Dev is Dev2 ) SetDev ( (Dev2) Dev ) ;
            
            return ;
            

            }

            modified on Wednesday, December 30, 2009 2:01 PM

            M Offline
            M Offline
            MollyTheCoder
            wrote on last edited by
            #11

            PIEBALDconsult wrote:

            Or perhaps SetDevice should handle determining which type of device it was passed.

            Wouldn't that mean creating a big SetDevice to handle all types, and just move the same code into SetDevice? Even if the code is a little less ugly thanks to is. Molly

            P 1 Reply Last reply
            0
            • M MollyTheCoder

              PIEBALDconsult wrote:

              Or perhaps SetDevice should handle determining which type of device it was passed.

              Wouldn't that mean creating a big SetDevice to handle all types, and just move the same code into SetDevice? Even if the code is a little less ugly thanks to is. Molly

              P Offline
              P Offline
              pbalaga
              wrote on last edited by
              #12

              Afaik, the 'is' keyword is not fully equal to x.GetType() == typeof(y) . Let's say I have: class A class B:A (B inheriting from A) Then following (pseudo-)code:

              b = new B();
              b is A

              returns true and

              b = new B();
              b.GetType() == typeof(A)

              returns false. The latter option checks for an exact type match.

              L M 2 Replies Last reply
              0
              • P pbalaga

                Afaik, the 'is' keyword is not fully equal to x.GetType() == typeof(y) . Let's say I have: class A class B:A (B inheriting from A) Then following (pseudo-)code:

                b = new B();
                b is A

                returns true and

                b = new B();
                b.GetType() == typeof(A)

                returns false. The latter option checks for an exact type match.

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #13

                Paul89 wrote:

                b = new B();
                b.GetType() == typeof(A);

                The above test the immediate types A.GetType() != B.GetType() && B.GetType() != A.GetType() for a better understanding try this:

                b = new B();
                a = new A();

                typeof(A).ToString();
                typeof(B).toString();

                a.GetType().toString();
                b.GetType().toString();

                However this works like the is keyword: B.IsSubclassOf(A) returns true -or- A.IsAssignableFrom(B) returns true On the is key word: 'An is expression evaluates to true if the provided expression is non-null, and the provided object can be cast to the provided type without causing an exception to be thrown.'

                modified on Wednesday, December 30, 2009 2:42 PM

                1 Reply Last reply
                0
                • P pbalaga

                  Afaik, the 'is' keyword is not fully equal to x.GetType() == typeof(y) . Let's say I have: class A class B:A (B inheriting from A) Then following (pseudo-)code:

                  b = new B();
                  b is A

                  returns true and

                  b = new B();
                  b.GetType() == typeof(A)

                  returns false. The latter option checks for an exact type match.

                  M Offline
                  M Offline
                  MollyTheCoder
                  wrote on last edited by
                  #14

                  Thanks! I think is works for the code I've got today, but you probably saved me from myself on some future project. Molly

                  P 1 Reply Last reply
                  0
                  • M MollyTheCoder

                    I know I'm missing something simple. I have a base class, Device, and a bunch of derived classes Dev1, Dev2... Depending on the type of device active at the moment, I have an abstract method for each derived type. Deciding which to use leads me to:

                    if (SelectedDevice.GetType() == typeof(Dev1))
                    {
                    SetDevice((Dev1)SelectedDevice);
                    }
                    else if (SelectedDevice.GetType() == typeof(Dev2))
                    {
                    SetDevice((Dev2)SelectedDevice);
                    }
                    else if ...

                    It works, but it just feels ugly. Molly

                    P Offline
                    P Offline
                    petercrab
                    wrote on last edited by
                    #15

                    looks fine to me.

                    1 Reply Last reply
                    0
                    • M MollyTheCoder

                      Thanks! I think is works for the code I've got today, but you probably saved me from myself on some future project. Molly

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #16

                      Or maybe not! :laugh:

                      M 1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        Or maybe not! :laugh:

                        M Offline
                        M Offline
                        MollyTheCoder
                        wrote on last edited by
                        #17

                        Who goes 400 pages deep (as of today) into a forum just to rub it in? :sigh:

                        P 1 Reply Last reply
                        0
                        • M MollyTheCoder

                          Who goes 400 pages deep (as of today) into a forum just to rub it in? :sigh:

                          P Offline
                          P Offline
                          PIEBALDconsult
                          wrote on last edited by
                          #18

                          That'd be me. :-D But I took a shortcut -- I saw on your profile that you had only posted a few questions, I checked those few. I didn't actually know I'd find this.

                          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