asadim

Assume "IClass" is an interface and "MyClass" implements it. What's the difference between the following 2:

1) IClass c = new MyClass();

2) MyClass c = new MyClass();

Also what's the purpose of casting to interfaces like this (taken from an app with Spring.NET framework):

ICultureConfigurationManager cultureConfigManager =

(ICultureConfigurationManager)SimpleObjectFactory.getObject("cultureConfigManager");

My understanding from the above is that, it creates an instance of ICultureConfig... and then assigns it to some object from the SimpleObjectFactory class which has been casted to the ICultureConfig... interface. Anyway, I don't understand how casting to an interface works and how useful it is.

Finally what's the difference between using the above method to cast to interfaces and using the "as" keyword

Thanks.



Re: Visual C# General Questions on interfaces

RizwanSharp

In OOP, this feature is called Polymorphism where super types can hold objects of any of its base types.

For example there is a super type IClass and 2 Sub types inhereted from it. At runtime you can initialize any object and put it in Super type.

Real world Example:

You need to provide support in your application that it may be able to interact with 2 databases, 1) SQL Server 2 Oracle.

You would do something like this:

Code Snippet

IDBConnection connection = null;

if(dBType == DBType.Sql)

{

connection = new SqlConnection();

return connection;

}

connection = new OracleConnection();

return connection;

Suppose both classes inherit IDConnection now, the resulting value will hold the object of one of its derived types so what ever object it holds, it'll call function on that specific type. Like connection.Open() etc.

I would recomend you to read about Polymorphism on internet and try to understand the concept.

I hope this will help...

Best Regards,

Rizwan aka RizwanSharp






Re: Visual C# General Questions on interfaces

vtortola

First case, create a MyClass object but you only can see the IClass methods, if you have more methods implements in MyClass you can't view it.

Consider the followin example:

Code Snippet

interface IClass

{

void method1();

void method2();

void method3();

}

class MyClass : IClass

{

public void myClassMethod1()

{

}

public void myClassMethod2()

{

}

public void myClassMethod3()

{

}

#region IClass Members

public void method1()

{

throw new Exception("The method or operation is not implemented.");

}

public void method2()

{

throw new Exception("The method or operation is not implemented.");

}

public void method3()

{

throw new Exception("The method or operation is not implemented.");

}

#endregion

}

Code Snippet

IClass ic = new MyClass();

ic.method1();

ic.method2();

ic.method3();

// But you can't access to myClassMethodX Wink

MyClass mc = new MyClass();

mc.method1();

mc.method2();

mc.method3();

mc.myClassMethod1();

mc.myClassMethod2();

mc.myClassMethod3();

Regards.






Re: Visual C# General Questions on interfaces

OmegaMan

asadim wrote:

Finally what's the difference between using the above method to cast to interfaces and using the "as" keyword

Thanks.



You may be thinking of this from the wrong angle. All an interface does is provide a contract of operations. You as the consumer may not care if the car you buy is a hybrid, gas or electric, you just want an ICar which gets you from point A to point B.

With that said, if one is creating an object factory to return items to consumers, the will get an interface only. The consumers may or may not necessarily know about the original object. If the consumers don't care about the original object, that allows the factory producer in the future to swap out to a different object without problems. Hence having the consumer not know, is a good thing...

The as is used when the consumer knows that he/she is getting a specific object and the as allows for a non exception type test of the object such as

ICar ic = Factory.GetCar();
myHybrid mo = ic as myHybrid;

if (mo == null) // try assigning it to a myGasCar

The ability to change objects but remain true to a contract (interface) is quite a useful thing to both the consumer and creator.