tags:

views:

1703

answers:

7

Is it possible to do like this

interface IDBBase {

     DataTable getDataTableSql(DataTable curTable,IDbCommand cmd);
     ...
}

class DBBase : IDBBase {

     public DataTable getDataTableSql(DataTable curTable, SqlCommand cmd) {
         ...
     }
}
+3  A: 

No it's not possible. Method should have same signature that one declared in the interface.

However you can use type parameter constraints:

interface IDBClass<T> where T:IDbCommand
{
    void Test(T cmd);
}

class DBClass:IDBClass<SqlCommand>
{
    public void Test(SqlCommand cmd)
    {
    }
}
aku
+1  A: 

Try compiling it. The compiler will report an error if DBBase doesn't implement IDBBase.

Brannon
+1  A: 

No, it's not possible. I tried compiling this:

interface Interface1 { }
class Class1 : Interface1 {}

interface Interface2 { void Foo(Interface1 i1);}
class Class2 : Interface2 {void Foo(Class1 c1) {}}

And I got this error:

'Class2' does not implement interface member 'Interface2.Foo(Interface1)'

Mark Cidade
+3  A: 

Covariance and contravariance are not widely supported as of C# 3.0, except for assigning method groups to delegates. You can emulate it a bit by using private interface implementation and call public method with more specific parameters:

class DBBase : IDBBase {

    DataTable IDBBase.getDataTableSql(DataTable curTable, IDbCommand cmd) {
         return getDataTableSql(curTable, (SqlCommand)cmd); // of course you should do some type checks
     }

     public DataTable getDataTableSql(DataTable curTable, SqlCommand cmd) {
         ...
     }
}
Ilya Ryzhenkov
+1  A: 

@marxidad,

Did you ever read the question?

No wonder this code can be compiled:

interface Interface2 { void Foo(Interface1 i1);}
class Class2 {void Foo(Class1 c1) {}}

No spend some time to read question, and try to compile this

interface Interface2 { void Foo(Interface1 i1);}
class Class2:Interface2 {void Foo(Class1 c1) {}}
aku
A: 

Why the downvotes on the correct answeres ?

It's NOT possible - the example from @marxidad is wrong - he forgot to do : Class2 : Interface2

sirrocco
A: 

Ok,to make the question more clear I want to use the interface to implement to d/t providers (MS-SQL,Oracle...) ,in it there are some signatures to be implemented in the corresponding classes that implement it.I also tried like this ?

genClass"<"typeOj">"

{

typeOj instOj;

public  genClass(typeOj o)


{      instOj=o  ;    }


public typeOj getType()

{        return instOj;    }

interface IDBBase {

 DataTable getDataTableSql(DataTable curTable,genClass<idcommand> cmd);
 ...

}

class DBBase : IDBBase {

 public DataTable getDataTableSql(DataTable curTable, genClass<SqlCommand> cmd)

{ ... }

}