Results 1 to 10 of 10

Thread: [RESOLVED] Interfaces

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Location
    Coming to a keyboard near you.
    Posts
    79

    Resolved [RESOLVED] Interfaces

    Alright - I need help with interfaces.

    Not sure I get how they benefit me. Please don't respond with MSDN, been there done that. Can't seem to get my hands around the concept for some reason... I am past reading, I need a working example. Sometimes I get things better when I walk the code.

    Anyone got a simple example they could share? Trying to see how to create and implement an interface as well as why you would want to use an interface.
    Tenebrosity
    "But the plans were on display ..."
    "On display? I eventually had to go down to the cellar to find them, With a torch."
    "That's the display department. The lights had probably gone out."
    "So had the stairs."
    "But look, you found the notice didn't you?"
    "Yes," said Arthur, "yes I did. It was on display in the bottom of a locked filing cabinet stuck in a disused lavatory with a sign on the door saying Beware of the Leopard."

  2. #2
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Interfaces

    I'm sure this falls under your "Don't point me to MSDN" category, but it is a great place to start. In my signature under webcasts, there's a link for modern software development. Inside of there, there's an episode on interfaces and the how and why you would use them.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Location
    Coming to a keyboard near you.
    Posts
    79

    Re: Interfaces

    Quote Originally Posted by sevenhalo
    I'm sure this falls under your "Don't point me to MSDN" category, but it is a great place to start. In my signature under webcasts, there's a link for modern software development. Inside of there, there's an episode on interfaces and the how and why you would use them.
    Have read the How to implement article and the walk threw at the MSDN. I agree its a great place to start, but now that I have started, I can not for the life of me get the code to work.

    I might be trying to hard... ever try to force something simple and you make it harder then it is? I might be to close to the problem to see I am the problem.
    Tenebrosity
    "But the plans were on display ..."
    "On display? I eventually had to go down to the cellar to find them, With a torch."
    "That's the display department. The lights had probably gone out."
    "So had the stairs."
    "But look, you found the notice didn't you?"
    "Yes," said Arthur, "yes I did. It was on display in the bottom of a locked filing cabinet stuck in a disused lavatory with a sign on the door saying Beware of the Leopard."

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Interfaces

    It comes handy when you're dealing with larger application and the architecture is important. You would create an interface which all classes must implement. For example, if you want all Entities to have a property called SerialNumber. You would enforce it with an Interface.

  5. #5
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Interfaces

    Sorry, I haven't been very clear all day today (heavy mix of tylenol and coffee).

    I meant the webcast was a good place to start. If you're really bored, watch the entire series (it's the one at the bottom). It's like a book on tape, but they show you what they're doing; so it's more like a lecture, but not in person and ya... You get what I'm saying.

    I haven't used interfaces too much, mainly because I've seen them as a connecting peice for development teams. They're lke a contract, saying that this is what our code will offer you and this is how you need to interact with it. I'm the sole developer for my company so I haven't dabbled much, but I should. Ecspecially for code reuse.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Location
    Coming to a keyboard near you.
    Posts
    79

    Re: Interfaces

    Quote Originally Posted by mendhak
    It comes handy when you're dealing with larger application and the architecture is important. You would create an interface which all classes must implement. For example, if you want all Entities to have a property called SerialNumber. You would enforce it with an Interface.
    Alright. So you implement the interface in your class. Does the interface have to live in the File.vb with that class or can it live in a different class and still be implemented?

    My class implements the interface and now creates a property called SerialNumber which must implement the interface.property.

    I think i see what your saying SevenHalo. Its just a contract or agreement that all properties called SerialNumber will be defined the same as the interface. So your controlling the structure of the code, and keeping the logic seperate. Right?

    Here is what I have been doing:

    VB Code:
    1. Public Class Math
    2.     Implements Class.M2
    3.  
    4.     Public Function Div(ByVal x As Integer, ByVal y As Integer) As Integer Implements M2.Div
    5.  
    6.         Div = x / y
    7.  
    8.     End Function
    9.  
    10. End Class
    11.  
    12. Public Interface M2
    13.     Function Div(ByVal x As Integer, ByVal y As Integer) As Integer
    14.  
    15. End Interface
    16.  
    17.  
    18.  
    19. In a different class and within a Sub I make this call:
    20.  
    21.         Dim MyDiv As M2 = New WindowsApplication1.Math
    22.  
    23.         x = 6
    24.         y = 2
    25.         MessageBox.Show(MyDiv(x, y))

    When I make a call to MyDiv it highlights in bluse and tells me:

    Interface 'Class.M2' cannot be indexed because it has no default property.

    What am I missing??
    Last edited by Tenebrosity; Dec 15th, 2005 at 05:25 PM.
    Tenebrosity
    "But the plans were on display ..."
    "On display? I eventually had to go down to the cellar to find them, With a torch."
    "That's the display department. The lights had probably gone out."
    "So had the stairs."
    "But look, you found the notice didn't you?"
    "Yes," said Arthur, "yes I did. It was on display in the bottom of a locked filing cabinet stuck in a disused lavatory with a sign on the door saying Beware of the Leopard."

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Interfaces

    Yes, the interface can exist in another .VB file.

    Yes, it means that all the implementing classes will implement the property specified there.

    And yes, you are controlling, more-or-less, the structure, and allowing developers to use their own logic.

    Another use for interfaces is when developers want to use multiple-inheritance; where one class derives from many different classes. Since multiple inheritance doesn't exist in .NET, you can inherit from one class, and implement 20 other classes.

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Interfaces

    MyDiv.Div(x,y)

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2002
    Location
    Coming to a keyboard near you.
    Posts
    79

    Re: Interfaces

    Quote Originally Posted by mendhak
    MyDiv.Div(x,y)
    Ok - I need a break. I was to close to the problem. Thanks for pointing out me being an idiot.

    Think I have a better understanding of Interfaces now, but wil have to read up on multiple-inheritance. Currently working as a single developer for a company, so I don't get a chance to talk shop like this much.

    Thanks again to both of you. I would call this thread resolved.
    Tenebrosity
    "But the plans were on display ..."
    "On display? I eventually had to go down to the cellar to find them, With a torch."
    "That's the display department. The lights had probably gone out."
    "So had the stairs."
    "But look, you found the notice didn't you?"
    "Yes," said Arthur, "yes I did. It was on display in the bottom of a locked filing cabinet stuck in a disused lavatory with a sign on the door saying Beware of the Leopard."

  10. #10
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] Interfaces

    You're welcome.

    I would call this thread a unicycling chimpanzee, but you can call it resolved if you like.

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