|
-
Jan 21st, 2010, 07:44 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Can somebody explain to me why I should use interfaces?
Hi all,
I have read some articles and tutorials about interfaces but I don't understand why you shoul use them. As far as I know they don't add any functionality? They can just be used to make sure you don't forget to add some property's or methods when creating a class.
Can somebody explain or provide me a link with a good example about when to use interfaces?
-
Jan 21st, 2010, 07:53 AM
#2
Re: Can somebody explain to me why I should use interfaces?
 Originally Posted by gonzalioz
Hi all,
I have read some articles and tutorials about interfaces but I don't understand why you shoul use them. As far as I know they don't add any functionality? They can just be used to make sure you don't forget to add some property's or methods when creating a class.
Can somebody explain or provide me a link with a good example about when to use interfaces?
You have covered some of the key points about interfaces. Have a look at the link below for some more info.
http://www.daniweb.com/forums/thread114364.html#
-
Jan 21st, 2010, 07:56 AM
#3
Re: Can somebody explain to me why I should use interfaces?
The true power of interfaces is being able to do this for example:
vb.net Code:
Public Sub DisplayAllElements(ByVal col As IEnumerable) For Each obj As Object In col MessageBox.Show(obj.ToString) Next End Sub
The method accepts an argument of type IEnumerable. Actually, what this means is that it will accept any type that implements IEnumerable. As such, it will accept arrays, ArrayLists, generic Lists, collections, etc.
This makes interfaces very useful sometimes, because you don't have to convert a List to an array (for example), as both are accepted. As long as the method does nothing more than iterate through the list, it does not need to know whether the argument is an array, list, collection, etc.
Basically, an interface can be used to tell 'the compiler' (and you, really) that some unknown instance of some unknown class must at least implement a few methods or properties.
I was in the situation just yesterday where I needed an interface. I am building a 'database engine' that works with 'entity classes'. An entity class is just a class with a property for every database attribute for some table.
The idea is that I can add a new entity class at a later stage, so that my engine can work with it. This is obviously not going to work by itself, because the engine (written now) cannot know anything about the entity class (not written yet). So, I have my entity class implement an IEntity interface which dictates it to have at least an Id property.
Now, the engine still doesn't know which class it is dealing with, but it does know that, whatever class it is, it has a property Id, so I can use that property.
Last edited by NickThissen; Jan 21st, 2010 at 08:05 AM.
-
Jan 21st, 2010, 08:00 AM
#4
Thread Starter
Hyperactive Member
Re: Can somebody explain to me why I should use interfaces?
Thanks guys! That's brilliant.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|