IDbCommand.Parameters and Explict Interface Implementation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon

    #1

    IDbCommand.Parameters and Explict Interface Implementation

    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.IDa taParameterColl ection Implements
    Systen.Data.IDb Command.Paramet ers

    Whereas what I want to return is the usual
    SqlClient.SqlPa rameterCollecti on 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.sql client.SqlParam eterCollection

    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
    SqlParamaterCol lection 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

    #2
    Re: IDbCommand.Para meters and Explict Interface Implementation

    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 SqlParameterCol lection.

    So I have


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

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

    Simon wrote:[color=blue]
    > 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.IDa taParameterColl ection Implements
    > Systen.Data.IDb Command.Paramet ers
    >
    > Whereas what I want to return is the usual
    > SqlClient.SqlPa rameterCollecti on 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.sql client.SqlParam eterCollection
    >
    > 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
    > SqlParamaterCol lection 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.[/color]

    Comment

    Working...