Interface question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • george r smith

    #1

    Interface question

    I thought that you it is forbidden to create an instance of an interface.

    But a help example (search for Explicit Interface Implementation Sample)
    from visual studio has the following sample code:

    // Declare an instance of the English units interface:
    IEnglishDimensi ons eDimensions = (IEnglishDimens ions) myBox;
    // Declare an instance of the metric units interface:
    IMetricDimensio ns mDimensions = (IMetricDimensi ons) myBox;

    So is this a mistatement - will someone put into words what the above
    statements are doing.
    thanks
    grs


  • Tu-Thach

    #2
    Interface question

    These statements do not create instances of the
    interfaces. What they do is cast to the interfaces. So,
    myBox instance probably implements both IEnglishDimensi ons
    and IMetricDimensio ns in its class definition.

    Tu-Thach

    [color=blue]
    >-----Original Message-----
    >I thought that you it is forbidden to create an instance[/color]
    of an interface.[color=blue]
    >
    >But a help example (search for Explicit Interface[/color]
    Implementation Sample)[color=blue]
    >from visual studio has the following sample code:
    >
    >// Declare an instance of the English units interface:
    >IEnglishDimens ions eDimensions = (IEnglishDimens ions)[/color]
    myBox;[color=blue]
    >// Declare an instance of the metric units interface:
    >IMetricDimensi ons mDimensions = (IMetricDimensi ons) myBox;
    >
    >So is this a mistatement - will someone put into words[/color]
    what the above[color=blue]
    >statements are doing.
    >thanks
    >grs
    >
    >
    >.
    >[/color]

    Comment

    • george r smith

      #3
      Re: Interface question

      That is what I thought ("cast to the interface") but when you see an example
      in the IDE help files it makes you ponder and uncertain.
      [color=blue]
      > These statements do not create instances of the
      > interfaces. What they do is cast to the interfaces. So,
      > myBox instance probably implements both IEnglishDimensi ons
      > and IMetricDimensio ns in its class definition.[/color]


      Comment

      • Jeffrey Tan[MSFT]

        #4
        Re: Interface question


        Hi George,

        Thanks for posting in this group.
        The implement of interface must be placed in the inherited class. And the
        implementation of interface has 2 types: explicit and implicit.
        When explicit implement the interface method, you should specify the
        interface name with a dot before the method name.
        This can be used to distinguish 2 different interface's methods
        implementation. (May have the same name)
        And the explicit implemented interface method can not be directly accessed
        from the class instance, you must convert the class instance to certain
        instance and then invoke this method.
        For your code snippet, I think your myBox implemented both
        IEnglishDimensi ons and IMetricDimensio ns, then if you want to use the
        methods of IEnglishDimensi ons, you'd better cast myBox into
        IEnglishDimensi ons and invoke its methods.(Whethe r the cast is a must
        depands on whether you implement IEnglishDimensi ons explicit or implicit).
        The same is for IMetricDimensio ns.
        These 2 behavior did not create an instance of any interface, they just get
        the instance of the interface(which was contained in class instance), and
        use it. So there is no mistatement.

        For more information of "Explicit Interface Implementation" , you can refer
        to the tutorial below:
        http://msdn.microsoft.com/library/de...us/csref/html/
        vcwlkexplicitin terfaceimplemen tationtutorial. asp

        If you still have anything unclear, please feel free to tell me.
        Have nice day!

        Best regards,
        Jeffrey Tan
        Microsoft Online Partner Support
        Get Secure! - www.microsoft.com/security
        This posting is provided "as is" with no warranties and confers no rights.

        Comment

        • Jeff Louie

          #5
          Re: Interface question

          George... Except for strings or factory methods, you use the key word
          new to create an instance of an class. eDimensions is a variable that
          contains a reference of type IEnglishDimensi ons that is totally separate
          from the actual object of class MyBox. So references have type and
          objects have class. More here:

          Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


          Regards,
          Jeff[color=blue]
          >I thought that you it is forbidden to create an instance of an[/color]
          interface.<


          *** Sent via Developersdex http://www.developersdex.com ***
          Don't just participate in USENET...get rewarded for it!

          Comment

          • george r smith

            #6
            Re: Interface question

            Jeff,
            WHAT A GREAT "mantra", "Objects have class, references have type."
            Thanks for the reply and thanks for the statement - sure helps to have a
            catchy phrase
            to aid the memory.
            grs


            "Jeff Louie" <anonymous@devd ex.com> wrote in message
            news:%23prva69r DHA.1872@TK2MSF TNGP09.phx.gbl. ..[color=blue]
            > George... Except for strings or factory methods, you use the key word
            > new to create an instance of an class. eDimensions is a variable that
            > contains a reference of type IEnglishDimensi ons that is totally separate
            > from the actual object of class MyBox. So references have type and
            > objects have class. More here:
            >
            > http://www.geocities.com/jeff_louie/OOP/oop6.htm
            >
            > Regards,
            > Jeff[color=green]
            > >I thought that you it is forbidden to create an instance of an[/color]
            > interface.<
            >
            >
            > *** Sent via Developersdex http://www.developersdex.com ***
            > Don't just participate in USENET...get rewarded for it![/color]


            Comment

            Working...