IDbCommand.Parameters and Explict Interface Implementation

Discussion in 'Microsoft VB .NET' started by Simon, Jun 7, 2006.

  1. Simon

    Simon Guest

    Hi All,

    I wanted to inherit a new SQLCommand (mostly to set default and ensure
    consistancy), but the class is NotInheritable, so instead I created a
    class implementing IDbCommand.

    So far so good. The problem I'm having is with the definition of the
    parameters (specifically the return type). IDbCommand defines them as

    public readonly property parameters() as
    system.data.IDataParameterCollection Implements
    Systen.Data.IDbCommand.Parameters

    Whereas what I want to return is the usual
    SqlClient.SqlParameterCollection for my derived classes. Seemingly
    this is possible in C# via something called "Explicit Interface
    Implentation" - a feature that seems to be missing from VB.NET (though
    I accept that the VB interface model is more flexible).

    If instead I wish to define

    public readonly property Parameters() as
    system.data.sqlclient.SqlParameterCollection

    the compiler complains that the definitions cannot overload each other
    because only the return types are different.

    I presume that I can cast the IDataParameter version to a
    SqlParamaterCollection in the derived classes but that's a departure
    from normal practices and my goal here is to make things easier down
    the line.

    Has anyone come across this and how did you work around it? I've not
    used interfaces extensively before so am more than happy to be told I'm
    overlooking something.
     
    Simon, Jun 7, 2006
    #1
    1. Advertisements

  2. Simon

    Simon Guest

    Don't you hate it when you forget something?

    I should add that the best solution that I've come up with so far is to
    create an intermediary class that overrides the default behaviour of
    Parameters and casts it to the SqlParameterCollection.

    So I have


    Base (implementing IDbCommand)
    |
    intermediary (overloads Parameters to return SqlParameterCollection)
    |
    derived classes

    This seems to work fine,but isn't what you'd call elegant.

    Simon wrote:
    > Hi All,
    >
    > I wanted to inherit a new SQLCommand (mostly to set default and ensure
    > consistancy), but the class is NotInheritable, so instead I created a
    > class implementing IDbCommand.
    >
    > So far so good. The problem I'm having is with the definition of the
    > parameters (specifically the return type). IDbCommand defines them as
    >
    > public readonly property parameters() as
    > system.data.IDataParameterCollection Implements
    > Systen.Data.IDbCommand.Parameters
    >
    > Whereas what I want to return is the usual
    > SqlClient.SqlParameterCollection for my derived classes. Seemingly
    > this is possible in C# via something called "Explicit Interface
    > Implentation" - a feature that seems to be missing from VB.NET (though
    > I accept that the VB interface model is more flexible).
    >
    > If instead I wish to define
    >
    > public readonly property Parameters() as
    > system.data.sqlclient.SqlParameterCollection
    >
    > the compiler complains that the definitions cannot overload each other
    > because only the return types are different.
    >
    > I presume that I can cast the IDataParameter version to a
    > SqlParamaterCollection in the derived classes but that's a departure
    > from normal practices and my goal here is to make things easier down
    > the line.
    >
    > Has anyone come across this and how did you work around it? I've not
    > used interfaces extensively before so am more than happy to be told I'm
    > overlooking something.
     
    Simon, Jun 7, 2006
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Özden Irmak

    Error on implementation of one method of an interface?

    Özden Irmak, Dec 11, 2003, in forum: Microsoft VB .NET
    Replies:
    2
    Views:
    287
    Özden Irmak
    Dec 11, 2003
  2. Guest

    Vb.NET Interface implementation

    Guest, Jan 22, 2004, in forum: Microsoft VB .NET
    Replies:
    4
    Views:
    407
    Peter Huang
    Jan 27, 2004
  3. Rob
    Replies:
    2
    Views:
    503
  4. Hayato Iriumi

    Check Interface Implementation of a Type

    Hayato Iriumi, Apr 1, 2006, in forum: Microsoft VB .NET
    Replies:
    4
    Views:
    300
    Hayato Iriumi
    Apr 2, 2006
  5. Christopher Lusardi
    Replies:
    6
    Views:
    300
    Guest
    Jun 1, 2006
Loading...

Share This Page