passage in a book

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi,

i was reading a vb.net book that said the following regarding:

Interfaces allow you to program using methods on the interface rather than
methods on the object?

How is this a benefit in lamen's terms?

thanks,
mj
 
It basically means that it allows you to look at the object in terms of its
interface - and thus be guaranteed that it has a particular method.

So let's say you have classes A, B, C. You will have a collection of
objects, and each object in the collection will be an instance of one of
those. You can either:
a) Look at each object in the collection, figure out if it's an A, B or C.
Then cast it to the right type, and call method DoSomething on it
b) Make each class implement ISomeInterface which has a method DoSomething.
Then just cast each object to an ISomeInterface and call DoSomething.

Which is easier?

Consider down the road, you add a class D. Scenario 'a', you would have to
change the code for your collection to now deal with a class D. Scenario
'b', you don't need to do anything, as long as class D implements
ISomeInterface.
 
thanks for the explanation. what do you mean when you said:

Then just cast each object to an ISomeInterface and call DoSomething.

how would the syntax look for that line?

mj
 
(ISomeInterface)myObj where myObj is some object that you have that
implements this interface. Otherwise, this code would throw an exception.
 
Or, perhaps more relevantly to ...dotnet.languages.*vb* :

DirectCast( myObj, ISomeInterface )

unless you know something we don't ... ;-)

Regards,
Phill W.
 
Thank you very much. this helped a lot.

Phill. W said:
Or, perhaps more relevantly to ...dotnet.languages.*vb* :

DirectCast( myObj, ISomeInterface )

unless you know something we don't ... ;-)

Regards,
Phill W.
 
Would this be along the same lines as APIs?

Marina said:
It basically means that it allows you to look at the object in terms of its
interface - and thus be guaranteed that it has a particular method.

So let's say you have classes A, B, C. You will have a collection of
objects, and each object in the collection will be an instance of one of
those. You can either:
a) Look at each object in the collection, figure out if it's an A, B or C.
Then cast it to the right type, and call method DoSomething on it
b) Make each class implement ISomeInterface which has a method DoSomething.
Then just cast each object to an ISomeInterface and call DoSomething.

Which is easier?

Consider down the road, you add a class D. Scenario 'a', you would have to
change the code for your collection to now deal with a class D. Scenario
'b', you don't need to do anything, as long as class D implements
ISomeInterface.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top