Azurewrath

Hi all,

What's the point of casting to interfaces, when you can invoke the members directly, like so:

Box b = new Box();

b.Extrude();
b.Chamfer();



instead of:

IEditable ed = b as IEditable;
b.Extrude ( );
b.Chamfer ( );

Since intellisense shows me what I can access, I don't have to worry about using nonexisiting members





Thanks,
Aw


Re: Visual C# Language Casting to interfaces

Oscarfh

First: you can't compile that because you can't intantiate interfaces.
What you can do is the following:
IEditable ed = new Box();
This is nice because if you decide that you don't want to use Box anymore, and want to use the new class Door (assuming it implements IEditable), you'll only need to change the new statement (otherwise you would need to change all the methods that Door doesn't implements, that are not declared in the IEditable interface).

Notice: once you do this, you can only use the methods that the interface declares, if you try to use a Box method that is not declared in the IEditable, the compiler will generate an error.

Other thing that somethimes is nice is having a generic list of an interface, like LinkedList<IEditable>, so you can have Box and Door classes in this list.

hope that helps,
Oscar




Re: Visual C# Language Casting to interfaces

Azurewrath

Hi,

The last 2 lines were supposed to be:

ed.Extrude ( );
ed.Chamfer ( );

But Box is a class. Are you sure it can't compile Because it works here Smile

You reply is helpful, thanks alot for the advice on interfaces.




Aw




Re: Visual C# Language Casting to interfaces

BalajiPrabu

Hi,

Casting to interface helps a lot. Consider this case as example.

You are providing the user with a DataProvider Assembly which can read data from both SQL database and from Oracle or other 3rd party database.

We should not force user to write two implementation for SQLDataReader and OLEDataReader. This will make lot of duplicate Code.

What you can do is, in your DataProvider assembly you can detect the type of Connection and use appropriate DataReader and send them as IDataReader to the use. I hope this example should help to to understand the use of Interface

Sample Code.

DataProvider Assembly

public class ConnectionClass

{

SqlConnection _sqlCon;

SqlCommand _sqlCmd;

SqlDataReader _sqlDR;

OleDbConnection _oleCon;

OleDbCommand _oleCmd;

OleDbDataReader _oleDR;

bool bIsSQL;

public ConnectionClass()

{

//Use your logic to read Connectionstring from config file and set bIsSQL to true if it is sqlcon;

//Also initialise all connection,Commands and DataReaders

//This is sample code so it will not have actual implementation

bIsSQL = true;

}

//

public IDataReader ExecuteQuery(string sQuery)

{

if (sQuery == null || sQuery.Length == 0)

return null;

if (bIsSQL)

{

_sqlCmd.CommandText = sQuery;

_sqlDR = _sqlCmd.ExecuteReader();

//Return SQLDataReader as IDataReader if the type of DataBase is SQL. So that in client app, they can get IDataReader and use the methods

return _sqlDR as IDataReader;

}

else

{

_oleCmd.CommandText = sQuery;

_oleDR = _oleCmd.ExecuteReader();

//Return OleDBReader as IDataReader if the type of DataBase is SQL. So that in client app, they can get IDataReader and use the methods

return _oleDR as IDataReader;

}

}

}

************************************

Client Assembly

public class Employee

{

public void GetEmployeeDetails()

{

//In your Eg , you have a config file which have the connetion string and DB detials (SQL or Access or Oracle)

//In consuming class(Employee) you should not write logic / Methods / Classes that are specific to type of Database you are using.

string sQuery = "Select * from employee";

ConnectionClass oConnection = new ConnectionClass();

IDataReader IOutputReader = null;

//As in this consuming application we dont know what type of DB we are using,

//we are creating IDataReader object and using the methods exposed in that Interface

IOutputReader = oConnection.ExecuteQuery(sQuery);

string sEmployeeName;

string sLocation;

//write your logic to read data. This is only Eg to show the use of Interface.

//so it does not contain any coding startands or best practices

sEmployeeName = IOutputReader["EmployeeName"].ToString();

sLocation = IOutputReader["Location"].ToString();

}

}

This is only a small sample of Interface usage. There are other lot of benefits in casting to Interface. You will get more benifit out of it if you develop framework kind of applications.

Cheers,

Balaji Prabu






Re: Visual C# Language Casting to interfaces

Azurewrath

Thanks alot man! Makes sense.

But say I want to implement an interface, how can I know which method it has available ie IComparable, etc




Thanks,
Aw




Re: Visual C# Language Casting to interfaces

timvw

You'll have to look it up..

( If you're using Visual Studio, you could simply move your mouse over the Interface, and press shift+alt+f10 so that it generates stubs for all the required members...)





Re: Visual C# Language Casting to interfaces

Azurewrath

Thanks man for the tip, it's great!




Aw