Interfaces

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Duncan  McNutt .[FTSE]

    #16
    Re: Interfaces

    I just got confused because a small code project i saw as an example used
    it in the class but never elsewhere, just seemed pointless for that example.

    --

    Duncan McNutt
    Microsoft Product Deactivation Team
    --


    "Jon Skeet" <skeet@pobox.co m> wrote in message
    news:MPG.19ac46 6f815bf62c98a34 0@news.microsof t.com...[color=blue]
    > Duncan McNutt .[FTSE] <pitmaster@127. 0.0.701> wrote:[color=green]
    > > Why does code derive from for example, IComparer
    > >
    > > ie,
    > >
    > > class SomeClass : IComparer
    > > {
    > > public SomeClass()
    > > {
    > > }
    > >
    > > public int Compare(object x, object y)
    > > {
    > > return someInt;
    > > }
    > > }
    > >
    > >
    > > When it just uses that and could have done without deriving ICompare[/color][/color]
    and[color=blue][color=green]
    > > got the same result.[/color]
    >
    > No you didn't. You can't use an instance of SomeClass to provide the
    > comparisons for Array.Sort, for instance, unless is implements
    > IComparer.
    >
    > --
    > Jon Skeet - <skeet@pobox.co m>
    > http://www.pobox.com/~skeet/
    > If replying to the group, please do not mail me too[/color]


    Comment

    • Peter Vidler

      #17
      Re: Interfaces

      Hi,
      [color=blue]
      > And I can use multiple interfaces on a class? i.,e
      >
      >
      > class SomeClass : ICompare , ISomethingOrOth er[/color]

      Yes that's fine.

      [color=blue]
      > and then implement both or just one?[/color]

      No you must implement all members of both. Although you can use explicit
      implementation:

      class SomeClass : IComparer
      {
      public int IComparer.Compa re( object x, object y )
      {
      // ...
      }
      }

      This will mean that the Compare method is no longer available from the
      SomeClass reference, but can be used when you cast to an IComparer
      reference:

      SomeClass c = new SomeClass();
      c.Compare(...); // error
      ICompare comp = c as ICompare;
      comp.Compare(.. .); // fine
      ( ( ICompare )c ).Compare(...); // fine

      This is useful when you don't want to clutter up the class with too many
      public methods (*cough* Java *cough* ;) ), but you still want the
      interface's functionality. It is required to do it this way if two or more
      interfaces have members with the same signature (name and parameters).
      [color=blue]
      > can I also extend that by having methods that arnt in that
      > interface yet still pass it into those CompareStuff (IComparer[]..
      > blah[/color]

      You can still add your own methods, and still use a function that takes an
      IComparer. The reference to IComparer (which you will be passing to the
      CompareStuff method) will only have access to the IComparer functionality,
      however.
      [color=blue]
      > even tho it has the interface, and more methods not specified by
      > that interface, if so, what happens if 2 interfaces have similar
      > methods?[/color]

      See above. If there's ever any confusion over a c# feature I always look at
      this webpage first:

      Hi. I'm Jon Jagger, director of software at Kosli. I built cyber-dojo, the place teams practice programming.


      Pete


      Comment

      • Peter Vidler

        #18
        Re: Interfaces

        Not sure if this came through okay, so here it is again with some added
        detail:

        Hi,
        [color=blue]
        > And I can use multiple interfaces on a class? i.,e
        >
        >
        > class SomeClass : ICompare , ISomethingOrOth er[/color]

        Yes that's fine.

        [color=blue]
        > and then implement both or just one?[/color]

        No you must implement all members of both. Although you can use explicit
        implementation:

        class SomeClass : IComparer
        {
        // non-public explicit implementation
        int IComparer.Compa re( object x, object y )
        {
        // ...
        }
        }

        This will mean that the Compare method is no longer available from the
        SomeClass reference, but can be used when you cast to an IComparer
        reference:

        SomeClass c = new SomeClass();
        c.Compare(...); // error
        IComparer comp = c as IComparer;
        comp.Compare(.. .); // fine
        ( ( IComparer )c ).Compare(...); // fine

        This is useful when you don't want to clutter up the class with too many
        public methods (*cough* Java *cough* ;) ), but you still want the
        interface's functionality. It is required to do it this way if two or more
        interfaces have members with the same signature (name and parameters).

        There's also language support for interfaces. If you implement the
        IDisposable interface you can use a "using" statement. This is because this:

        using( SomeClass c = new SomeClass() )
        {
        // do stuff
        }

        Compiles to the equivalent of this:

        SomeClass c = new SomeClass();
        try
        {
        // do stuff
        }
        finally
        {
        if ( c != null )
        {
        ( ( IDisposable )c ).Dispose();
        }
        }

        There is also language support for IEnumerable which allows your classes to
        be used in a foreach statement.
        [color=blue]
        > can I also extend that by having methods that arnt in that
        > interface yet still pass it into those CompareStuff (IComparer[]..
        > blah[/color]

        You can still add your own methods, and still use a function that takes an
        IComparer. The reference to IComparer (which you will be passing to the
        CompareStuff method) will only have access to the IComparer functionality,
        however.
        [color=blue]
        > even tho it has the interface, and more methods not specified by
        > that interface, if so, what happens if 2 interfaces have similar
        > methods?[/color]

        See above. If there's ever any confusion over a c# feature I always look at
        this webpage first:

        Hi. I'm Jon Jagger, director of software at Kosli. I built cyber-dojo, the place teams practice programming.


        Pete



        Comment

        • Frank Drebin

          #19
          Re: Interfaces

          That is the slick part about interfaces. You can define a variable of type
          "some interface" - and then instantiate it with ANY class that implements
          that interface. With the example I gave before.. Imagine you have an
          IDataAccess interface that defines ways that you can get data - and includes
          login and logout. Now imagine I have 3 classes "WebSvcAcce ss", "SQLServer"
          and "OfflineAcc ess" that all implement that interface.

          Let me stress the signifigance of this.

          The ENTIRE application is written to the IDataAccess interface, it has NO
          IDEA what those other classes are - and at RUN-time, the user chooses thier
          access type. So long as those classes have correctly implemented the
          interface - the whole app runs the same no matter what the connection type.

          So one time, I do this:

          IDataAccess objMainAccessOb ject = new WebSvcsAccess() ;
          -or-
          IDataAccess objMainAccessOb ject = new SQLServer();
          -or-
          IDataAccess objMainAccessOb ject = new OfflineAccess() ;

          And it is ONLY at that time that my application knows what kind of access
          they will be using. After that, the method of data access is *completely*
          abstracted. This is basically the "factory" design pattern (more or less).
          The whole rest of the app just interacts with objMainAccessOb ject as if it
          was the same all along. And in theory, you could even switch access types -
          like if you went from offline access to being plugged into the net. you
          could freeze the app, re-instantiate objMainAccessOb ject with new
          SQLServer() and pick up where you left off. It's really a hugely powerful
          concept.

          So you aren't creating an instance of an interface (because you can't) - but
          you can define a variable to be of an interfaces "type", and then
          instantiate it with an object that implements that interface.. this alone is
          such a GREAT feature of OO that you just couldn't do in VB6.. or that you
          could do in C++, but you could also easily work around this. It seems much
          easier to enforce this in a real strongly-typed environment like this...



          "Stan" <stanly12spamre move@yahoo.com. au> wrote in message
          news:OYmwrNrZDH A.1644@TK2MSFTN GP10.phx.gbl...[color=blue]
          > Nice to see this thread appear as I have my own interface based query to[/color]
          put[color=blue]
          > to the group. I was just reading up on an article that has some sample[/color]
          code[color=blue]
          > and was perplexed to see this line appear,
          >
          > ICodeCompiler loCompiler = new CSharpCodeProvi der().CreateCom piler();
          >
          > I believe this is similar to a sample that someone else has posted to this
          > thread. What confuses me is the fact that you are able to create an[/color]
          instance[color=blue]
          > of type interface. I would have thought that you would have to create a[/color]
          type[color=blue]
          > of something which implements the interface, since the interface defines
          > only exposed methods i.e. no implementation and doesn't even define
          > constructors. What is the base type of the instance in this case? Is it
          > simply an System.object type which is obliged to support the interface?[/color]
          I'm[color=blue]
          > thinking that must be it. I think this is different to specifying an
          > interface as a function argument, in which case we are saying that[/color]
          whatever[color=blue]
          > is passed must support the interface.
          >
          > " Duncan McNutt .[FTSE]" <pitmaster@127. 0.0.701> wrote in message
          > news:uLBzK$kZDH A.2668@TK2MSFTN GP09.phx.gbl...[color=green]
          > > Why does code derive from for example, IComparer
          > >
          > > ie,
          > >
          > > class SomeClass : IComparer
          > > {
          > > public SomeClass()
          > > {
          > > }
          > >
          > > public int Compare(object x, object y)
          > > {
          > > return someInt;
          > > }
          > > }
          > >
          > >
          > > When it just uses that and could have done without deriving ICompare[/color]
          > and[color=green]
          > > got the same result.
          > >
          > > class SomeClass
          > > {
          > > public SomeClass()
          > > {
          > > }
          > >
          > > public int Compare(object x, object y)
          > > {
          > > return someInt;
          > > }
          > > }
          > >
          > > --
          > >
          > > Duncan McNutt
          > > Microsoft Product Deactivation Team
          > > --
          > >
          > >
          > >[/color]
          >
          >[/color]


          Comment

          • Jon Skeet

            #20
            Re: Interfaces

            Stan <stanly12spamre move@yahoo.com. au> wrote:[color=blue]
            > Nice to see this thread appear as I have my own interface based query to put
            > to the group. I was just reading up on an article that has some sample code
            > and was perplexed to see this line appear,
            >
            > ICodeCompiler loCompiler = new CSharpCodeProvi der().CreateCom piler();
            >
            > I believe this is similar to a sample that someone else has posted to this
            > thread. What confuses me is the fact that you are able to create an instance
            > of type interface.[/color]

            You're not - just as the line:

            object x = new StringBuilder() ;

            is creating an instance of StringBuilder, not of Object - it's just
            that you can assign a reference of type StringBuilder to a variable of
            type Object. It's exactly the same deal with interfaces - you can
            assign a reference of any type which implements the interface to a
            variable whose type is the interface.

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            Working...