Class Interface

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2007
Posts: 48
Reputation: FallenPaladin is an unknown quantity at this point 
Solved Threads: 0
FallenPaladin FallenPaladin is offline Offline
Light Poster

Class Interface

 
0
  #1
Feb 11th, 2009
Hi I am slightly confused about interfaces for an inheritance structure. Firstly can anyone explain there purpose, secondly can anyone demonstrate there use.

I have created an application that uses reflection to dynamically instantiate classes and invoke methods within those classes. During research for this app I cam accross interfaces but was lost at there purpose and the syntax of there use.

I would be greatful if anyone can help. Thanks
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 8
Reputation: DrLod is an unknown quantity at this point 
Solved Threads: 1
DrLod DrLod is offline Offline
Newbie Poster

Re: Class Interface

 
0
  #2
Feb 12th, 2009
Originally Posted by FallenPaladin View Post
Hi I am slightly confused about interfaces for an inheritance structure. Firstly can anyone explain there purpose, secondly can anyone demonstrate there use.

I have created an application that uses reflection to dynamically instantiate classes and invoke methods within those classes. During research for this app I cam accross interfaces but was lost at there purpose and the syntax of there use.

I would be greatful if anyone can help. Thanks
You can use interfaces to structure up your programming when using polymorphism and to pass objects that are different but who all have some methods that are defined in the interface.

Example: http://www.codersource.net/csharp_tu...interface.html
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 2,414
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Solved Threads: 123
Team Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: Class Interface

 
0
  #3
Feb 12th, 2009
Just to add to this:

If you're using reflection frequently because you have several classes that have the same method, you could consider casting those objects up to a defined interface. Say you have two classes, ClassA and ClassB. Both classes have a method called Run(), which you've defined in an interface called IRunnable:

  1. public interface IRunnable
  2. {
  3. public void Run();
  4. }
  5.  
  6. ClassA a = new ClassA();
  7. ClassB b = new ClassB();
  8.  
  9. //casting up to IRunnable
  10. IRunnable aR = (IRunnable)a;
  11. IRunnable bR = (IRunnable)b;
  12.  
  13. //you could then pass these IRunnables to any method that takes an IRunnable as a parameter, for instance.
  14.  
  15. private static void MakeRun(IRunnable r)
  16. {
  17. r.Run();
  18. }
  19.  
  20. MakeRun(aR);
  21. MakeRun(bR);

...This is a really simple example, but I hope you get a good idea. You can do similar things with Interfaces that you can do with Reflection. I think it's cleaner to use Interfaces because you then have type-safety when executing those methods when returning values and inputting parameters.
Alex Cavnar, aka alc6379
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 48
Reputation: FallenPaladin is an unknown quantity at this point 
Solved Threads: 0
FallenPaladin FallenPaladin is offline Offline
Light Poster

Re: Class Interface

 
0
  #4
Feb 19th, 2009
Hi there thank you all for your input. I understand interfaces a bit better now, so thank you again.

Jon
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 128
Reputation: sivak has a little shameless behaviour in the past 
Solved Threads: 0
sivak sivak is offline Offline
Junior Poster

Re: Class Interface

 
0
  #5
Jun 5th, 2009
as u mentioned in the above example parameters are same ,methods also same..is it runtime polymorphisim? can we achieve polymorphisim thru interfaces
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC