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. Simple interface question...Help!!

Simple interface question...Help!!

Scheduled Pinned Locked Moved C#
questionhelptutorial
16 Posts 5 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.
  • C Offline
    C Offline
    Cormac M Redmond
    wrote on last edited by
    #1

    I'm a bit confused at something... Look at this code: ((ISupportInitialize)(this.trackBar)).EndInit(); There is no significance to this particular code, it's just an example. trackBar is an object that implements the ISupportInitialize interface. That interface only defines BeginInit() and EndInit(). Questions: 1. trackBar does NOT implement these methods at all - how come this is allowed? Obviously ISupportInitialize doesn't implement them either, as it is an interface. 2. How can we cast from trackBar to ISupportInitialize and call a method with no implementation? What is going on? Also, why cast at all if trackBar1 DID implement them? Also, when you do cast from an object that implements interface X, does that new casted object only contain the methods that X defines? I'm ashamed to not know this - and I'm sure it'll be very clear, but it's just going against the grain to what I know. Thanks, Cormac

    S S C C 4 Replies Last reply
    0
    • C Cormac M Redmond

      I'm a bit confused at something... Look at this code: ((ISupportInitialize)(this.trackBar)).EndInit(); There is no significance to this particular code, it's just an example. trackBar is an object that implements the ISupportInitialize interface. That interface only defines BeginInit() and EndInit(). Questions: 1. trackBar does NOT implement these methods at all - how come this is allowed? Obviously ISupportInitialize doesn't implement them either, as it is an interface. 2. How can we cast from trackBar to ISupportInitialize and call a method with no implementation? What is going on? Also, why cast at all if trackBar1 DID implement them? Also, when you do cast from an object that implements interface X, does that new casted object only contain the methods that X defines? I'm ashamed to not know this - and I'm sure it'll be very clear, but it's just going against the grain to what I know. Thanks, Cormac

      S Offline
      S Offline
      Shy Agam
      wrote on last edited by
      #2

      Cormac M Redmond wrote:

      trackBar does NOT implement these methods at all - how come this is allowed?

      I don't think it is allowed. I would say it should throw you an InvalidCastException. How does your TrackBar class declaration looks like?

      Cormac M Redmond wrote:

      Also, when you do cast from an object that implements interface X, does that new casted object only contain the methods that X defines?

      Yes. Boxing to a base class/interface hides the members of the derived class.

      C H C 3 Replies Last reply
      0
      • S Shy Agam

        Cormac M Redmond wrote:

        trackBar does NOT implement these methods at all - how come this is allowed?

        I don't think it is allowed. I would say it should throw you an InvalidCastException. How does your TrackBar class declaration looks like?

        Cormac M Redmond wrote:

        Also, when you do cast from an object that implements interface X, does that new casted object only contain the methods that X defines?

        Yes. Boxing to a base class/interface hides the members of the derived class.

        C Offline
        C Offline
        Cormac M Redmond
        wrote on last edited by
        #3

        Hi, It is allowed though - I found it it fully working code. Here it is also: http://msdn2.microsoft.com/en-us/library/system.componentmodel.isupportinitialize.aspx It's really wrecking my head :) Regards, Cormac Redmond

        S 1 Reply Last reply
        0
        • S Shy Agam

          Cormac M Redmond wrote:

          trackBar does NOT implement these methods at all - how come this is allowed?

          I don't think it is allowed. I would say it should throw you an InvalidCastException. How does your TrackBar class declaration looks like?

          Cormac M Redmond wrote:

          Also, when you do cast from an object that implements interface X, does that new casted object only contain the methods that X defines?

          Yes. Boxing to a base class/interface hides the members of the derived class.

          H Offline
          H Offline
          hofi1985
          wrote on last edited by
          #4

          imho you can cast an object to an interface even the methods aren't implemented. the code will compile. but if the runnig programm tries to execute this, you will get an System.InvalidCastException. cheers

          S 1 Reply Last reply
          0
          • C Cormac M Redmond

            Hi, It is allowed though - I found it it fully working code. Here it is also: http://msdn2.microsoft.com/en-us/library/system.componentmodel.isupportinitialize.aspx It's really wrecking my head :) Regards, Cormac Redmond

            S Offline
            S Offline
            Shy Agam
            wrote on last edited by
            #5

            Well... I thought that TrackBar is a class you created. After finding out it's located in System.Windows.Forms I checked its base types. If you open your Object Browser and do the same you'll see that TrackBar DOES implement ISupportInitialize. Regards, Shy.

            C 1 Reply Last reply
            0
            • C Cormac M Redmond

              I'm a bit confused at something... Look at this code: ((ISupportInitialize)(this.trackBar)).EndInit(); There is no significance to this particular code, it's just an example. trackBar is an object that implements the ISupportInitialize interface. That interface only defines BeginInit() and EndInit(). Questions: 1. trackBar does NOT implement these methods at all - how come this is allowed? Obviously ISupportInitialize doesn't implement them either, as it is an interface. 2. How can we cast from trackBar to ISupportInitialize and call a method with no implementation? What is going on? Also, why cast at all if trackBar1 DID implement them? Also, when you do cast from an object that implements interface X, does that new casted object only contain the methods that X defines? I'm ashamed to not know this - and I'm sure it'll be very clear, but it's just going against the grain to what I know. Thanks, Cormac

              S Offline
              S Offline
              Stefan Troschuetz
              wrote on last edited by
              #6

              Cormac M Redmond wrote:

              trackBar does NOT implement these methods at all

              What makes you think that? Here they are: TrackBar.BeginInit[^] TrackBar.EndInit[^]


              "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

              www.troschuetz.de

              1 Reply Last reply
              0
              • H hofi1985

                imho you can cast an object to an interface even the methods aren't implemented. the code will compile. but if the runnig programm tries to execute this, you will get an System.InvalidCastException. cheers

                S Offline
                S Offline
                Shy Agam
                wrote on last edited by
                #7

                That's correct.

                1 Reply Last reply
                0
                • S Shy Agam

                  Cormac M Redmond wrote:

                  trackBar does NOT implement these methods at all - how come this is allowed?

                  I don't think it is allowed. I would say it should throw you an InvalidCastException. How does your TrackBar class declaration looks like?

                  Cormac M Redmond wrote:

                  Also, when you do cast from an object that implements interface X, does that new casted object only contain the methods that X defines?

                  Yes. Boxing to a base class/interface hides the members of the derived class.

                  C Offline
                  C Offline
                  Colin Angus Mackay
                  wrote on last edited by
                  #8

                  shyagam wrote:

                  Yes. Boxing to a base class/interface hides the members of the derived class.

                  Boxing means accessing a value type via a reference. What you wanted to say is "casting". The two are different concepts, although boxing happens at the same time as a value type is cast to the base type.


                  Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

                  S 1 Reply Last reply
                  0
                  • C Cormac M Redmond

                    I'm a bit confused at something... Look at this code: ((ISupportInitialize)(this.trackBar)).EndInit(); There is no significance to this particular code, it's just an example. trackBar is an object that implements the ISupportInitialize interface. That interface only defines BeginInit() and EndInit(). Questions: 1. trackBar does NOT implement these methods at all - how come this is allowed? Obviously ISupportInitialize doesn't implement them either, as it is an interface. 2. How can we cast from trackBar to ISupportInitialize and call a method with no implementation? What is going on? Also, why cast at all if trackBar1 DID implement them? Also, when you do cast from an object that implements interface X, does that new casted object only contain the methods that X defines? I'm ashamed to not know this - and I'm sure it'll be very clear, but it's just going against the grain to what I know. Thanks, Cormac

                    C Offline
                    C Offline
                    Colin Angus Mackay
                    wrote on last edited by
                    #9

                    Cormac M Redmond wrote:

                    Also, when you do cast from an object that implements interface X, does that new casted object only contain the methods that X defines?

                    The object doesn't change. After you cast an object it is still the same object. It is just the reference to it that is different.


                    Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

                    C 1 Reply Last reply
                    0
                    • C Colin Angus Mackay

                      shyagam wrote:

                      Yes. Boxing to a base class/interface hides the members of the derived class.

                      Boxing means accessing a value type via a reference. What you wanted to say is "casting". The two are different concepts, although boxing happens at the same time as a value type is cast to the base type.


                      Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

                      S Offline
                      S Offline
                      Shy Agam
                      wrote on last edited by
                      #10

                      Hmm... I see... I'll remember that. :)

                      1 Reply Last reply
                      0
                      • S Shy Agam

                        Well... I thought that TrackBar is a class you created. After finding out it's located in System.Windows.Forms I checked its base types. If you open your Object Browser and do the same you'll see that TrackBar DOES implement ISupportInitialize. Regards, Shy.

                        C Offline
                        C Offline
                        Cormac M Redmond
                        wrote on last edited by
                        #11

                        That was a poor example then - the code I have in a project does NOT implement them. And if you're write about TrackBar...why cast to the interface? Why not just call the methods? Regards, Cormac

                        1 Reply Last reply
                        0
                        • C Cormac M Redmond

                          I'm a bit confused at something... Look at this code: ((ISupportInitialize)(this.trackBar)).EndInit(); There is no significance to this particular code, it's just an example. trackBar is an object that implements the ISupportInitialize interface. That interface only defines BeginInit() and EndInit(). Questions: 1. trackBar does NOT implement these methods at all - how come this is allowed? Obviously ISupportInitialize doesn't implement them either, as it is an interface. 2. How can we cast from trackBar to ISupportInitialize and call a method with no implementation? What is going on? Also, why cast at all if trackBar1 DID implement them? Also, when you do cast from an object that implements interface X, does that new casted object only contain the methods that X defines? I'm ashamed to not know this - and I'm sure it'll be very clear, but it's just going against the grain to what I know. Thanks, Cormac

                          C Offline
                          C Offline
                          Cormac M Redmond
                          wrote on last edited by
                          #12

                          ((ISupportInitialize) this.pictureBox1).BeginInit(); Ok...there pictureBox1 is a PictureBox and does not implement those methods. Can anyone explain? My first example was a bad one. Regards, Cormac

                          S 1 Reply Last reply
                          0
                          • C Colin Angus Mackay

                            Cormac M Redmond wrote:

                            Also, when you do cast from an object that implements interface X, does that new casted object only contain the methods that X defines?

                            The object doesn't change. After you cast an object it is still the same object. It is just the reference to it that is different.


                            Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

                            C Offline
                            C Offline
                            Cormac M Redmond
                            wrote on last edited by
                            #13

                            Sorry....I knew that - not being very clear today.

                            C 1 Reply Last reply
                            0
                            • C Cormac M Redmond

                              Sorry....I knew that - not being very clear today.

                              C Offline
                              C Offline
                              Colin Angus Mackay
                              wrote on last edited by
                              #14

                              No problem - we all have days like that from time to time. Earlier this week I sent an email round the development team at work telling them about this work around I'd found to get around the fact that such-and-such menu item wasn't available in a certain project type in Visual Studio. One of the devs in our other office wrote back and said, "do you mean the 8th item on the so-and-so menu?". For a whole day I'd been ripping my hair out trying to figure it out and it was staring at me in the face. I replied the only thing I could reply: "This year I shall be mostly going slightly mad". Also, in my uni' days I'd rip my hair out over compiler messages that I couldn't understand. I'd eventually find after several hours I'd missed a semi-colon. Luckily, today compilers are much better about point out missing semi-colons than the DOS version of Turbo C++ was.


                              Upcoming Scottish Developers events: * Glasgow: Tell us what you want to see in 2007 My: Website | Blog | Photos

                              1 Reply Last reply
                              0
                              • C Cormac M Redmond

                                ((ISupportInitialize) this.pictureBox1).BeginInit(); Ok...there pictureBox1 is a PictureBox and does not implement those methods. Can anyone explain? My first example was a bad one. Regards, Cormac

                                S Offline
                                S Offline
                                Stefan Troschuetz
                                wrote on last edited by
                                #15

                                In Framework 2.0 PictureBox implements both methods as explicit interface implementations; scroll down to the bottom of the following page: PictureBox Methods[^]. Some information on explicit interface implementations can be found here: Explicit interface member implementations[^]


                                "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                                www.troschuetz.de

                                C 1 Reply Last reply
                                0
                                • S Stefan Troschuetz

                                  In Framework 2.0 PictureBox implements both methods as explicit interface implementations; scroll down to the bottom of the following page: PictureBox Methods[^]. Some information on explicit interface implementations can be found here: Explicit interface member implementations[^]


                                  "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                                  www.troschuetz.de

                                  C Offline
                                  C Offline
                                  Cormac M Redmond
                                  wrote on last edited by
                                  #16

                                  Excellent - question answers. Thanks.

                                  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