Results 1 to 9 of 9

Thread: Interfaces

  1. #1

    Thread Starter
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314

    Interfaces

    Forgive my ignorance.... this is a rather embarrassing question.....

    I started out with VB6 a couple of years ago, just because I thought it was fun to make my own programs..... I then started support the program where I work, so I started to use it more frequently. The last few months I turned to .NET and all its object orientated "beauty". So fully object orientated programming is quite new to me.

    I can say that I fully understand the Polymorphic advantages with inheritance and its concept BUT .......

    I can't seem to grasp the concept of Interfaces 100%. How do they work? What are the polymorphic advantaged? Why and when would I create my own interface?

    I've read about it in 2 books, but I still can't really get there, so please, do not redirect me to a page filled with technical language, try to explain it as simple as possible........

    Thanks in advance

  2. #2
    Member
    Join Date
    May 2001
    Posts
    39

    My understanding

    Use interfaces when you have a lot of classes and them perform the same actions (ex: read and save), but the implementation of this actions differ form one to another.
    If the implementation is similar use abstract classes.

  3. #3

    Thread Starter
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    Thanks for the reply.... I think I still need it explained more thoroughly ....

  4. #4
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    An interface is simply a 'set of rules' that class must follow. In other words you declare the names and parameters for certain functions in your interface, but you don't write any code. When another class implements that interface, the class must as a minimum, add thos exact same functions to its class and code them to do something.

    Why is this useful you may ask? Well lets take a plug-in archetecture for your application for example. say you want to have plug-ins and your application expects a function called GetData(). How do you ensure that the plug-in has a function called GetData(). Well you can tell plug-in writers that they must impelement your interface and then trough some coding, you can determine wether the plug-in uses your interface and if it doesnt, dont load it.

    Look at COM component's as another example. To make an VB6 dll run in MTS, you have to implement the ObjectContext interface becasue it contains some events that MTS will try to fire off in your dll.

    In simplist terms, an interface is set of rules that a class must follow
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  5. #5
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    That a good example Mr. Anderson
    Dont gain the world and lose your soul

  6. #6

    Thread Starter
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    Thanks Cander, that helped alot.

    But the book I've read does not only talk about implementation, that I feel is what you're talking about, but also about interaction with the interface..... so I still think there is more to this matter..... isn't it?

    For example: One of the reason that you're not allowed to declare fields in an interface is to ensure that classes that interact through the interface do not have access to the internal data of an object. That has to mean that you have access to the other things, Properties, Methods.....?!?
    Last edited by Athley; Dec 3rd, 2002 at 01:11 PM.

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    One way to help see the usefulness of Interfaces is to look at how the framework uses them. Lets take IEnumerable and IEnumerator these interfaces are the contract that says your object can be used in a for..each loop. So when your code is compiled or executed, before the for..each the framework can check and say is this object allowed to be accessed in this way. SO it can check the object for the interface and see yes it can or no it can't. The same goes for some other common feaures like the Clone method, which is part of the ICloneable interface. A new one I like with .NEt is that youcan now bind to objects directly not just data (in things like a datagrid or combobox). BUT in order to bind to them then need to obey the contract for it, which is the IList interface or one of its derived versions.

    Since the object is known to have a certain set of methods and properties its kind of like a subset object, because all of these different object types can be converted to that interface and used for that purpose.

  8. #8

    Thread Starter
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    Thanks alot..... now I'm close.... (waiting for that %"!#!$ coin to fall through).... ...... don't let this stop you though, if you have more to tell that can help please share.

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    The main thing is just a structural implemention. Although to extend Cander's plugin example, you can also pass different object types into the same function that accepts a common interface as the parameter.

    VB Code:
    1. Public Interface ICommon
    2.    Property Name() As String
    3.    Function getID() As Integer
    4. End Interface
    5.  
    6. 'in a class
    7. Public Function Example(i as ICommon) As String
    8.    Return i.Name & i.getID()
    9. End Function
    10.  
    11. 'say that these 2 objects implement ICommon
    12. Dim Ex1 As New Example1
    13. Dim Ex2 As New SomethingElse
    14.  
    15. 'even though they are 2 different types they can be passed to the function because they implement the interface
    16. Msgbox(Me.Example(Ex1))
    17. Msgbox(Me.Example(Ex2))

    Really to understand them fully you'll just need to try them out a bit.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width