Results 1 to 4 of 4

Thread: "Executing" interface Methods

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    259

    "Executing" interface Methods

    Hello all,

    I am wonder what .Net does internally when you have an object cast as an interface it inherits and what happens when you call the interface's method.

    Example:
    ...
    IDBCommand.ExecuteReader(...);
    ...

    This will execute sql command with no issue against a IDBConnection, assuming everything else is setup correctly. Does anyone have any leads or good explainations on how this occurs?

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    259

    Re: "Executing" interface Methods

    Any one have any information on this at all?

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,538

    Re: "Executing" interface Methods

    well, this doesn't ever really get called: IDBCommand.ExecuteReader(...);

    what gets called is your_class_instance.ExecuteReader(...);

    and you don't inherit an interface... interfaces are implemented. Now, once it's been implemented, the implementing class can be inherited. But then you're inheriting the class, not the interface.

    Interfaces are nothing magical really. There's nothing special that goes on behind the scenes, not notably at any rate. The point of interfaces is to enforce a contract between caller and callee. If I know that a particular class implements ISomething then I can be guaranteed that when I call .DoSomething ... there will be a DoSomething method to be called. I don't care what DoSomething does, or if it even does something. All I know is that that method exists, and has the given signature. It's a way to allow for binding and prevents blind calls.

    Example: both OLEDB and SQLClient have an ExecuteReader method... that's because both classes implement the IDBCommand interface. That means I can Dim someDBCallee as IDBCommand ... and then use EITHER SQLClient OR OLEDB classes to create a command object... then I can just call someDBCallee.ExecuteReader ... w/o caring how it does it (Access vs SQL Server) ... all I know is that ExecuteReader will execute the command and return a Reader object....

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    259

    Re: "Executing" interface Methods

    Thank you for your response.

    Could you clairify how the CLR determines what the underlying object is and its implementation of the contract method?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width