Results 1 to 4 of 4

Thread: Using Implements

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 1999
    Location
    NY, USA.
    Posts
    240

    Question

    Hi,
    Can someone please explain to me what is the purpose and how can I use of VB's Implements. Also if I use it in my program what are the advantages and disadvantages. If possible an example would be gladly appreciated.

    Thanks !!
    Omar
    [email protected]
    http://omar.caribwalk.com
    To God Be The Glory

    I see Tech People ...

  2. #2
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Boulder, Colorado, USA
    Posts
    325
    Implements refers to giving multiple classes the same interface. I'm not very good at explaining it so I'll give you an example.

    Code:
    '' clsFruit.cls
    
    '' base class is clsFruit
    '' it has 1 member function as follows
    Public Function WhatAmI() As String
        
        WhatAmI = "All Fruits"
        
    End Function
    
    '' End of class clsFruit
    
    '' clsApple.cls
    
    '' now we have a second class (clsApple) which implements clsFruit
    
    '' implement class clsFruit
    '' it's like saying clsApple is a clsFruit
    Implements clsFruit
    
    '' redefine the function "WhatAmI"
    Private Function clsFruit_WhatAmI() As String
        
        clsFruit_WhatAmI = "Apple"
        
    End Function
    
    '' End of class clsApple
    
    '' now we can do the following
    
    Dim fruit as clsFruit
    
    Set fruit = new clsFruit
    
    '' this will return "All Fruits"
    Debug.Print fruit.WhatAmI()
    
    '' we can also do this:
    Set fruit = new clsApple
    
    '' this will return "Apple"
    Debug.Print fruit.WhatAmI()
    get it?
    -Shickadance

  3. #3
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    I get it

    I thought that was well done MrShickadance.

    I have not used it in my own code as yet (although I have used other class libraries which use Implements keyword).

    The general purpose of such a keyword is not very well supported in VB today. From what I understand VB7 will be providing much better Object Oriented Programming tools with three new keywords for that purpose.

    As MyShickadance said, Implements allows you to standardise related classes by providing you with the opportunity to specify a set of functions or subs that all related classes MUST implement (this is where you use the Implements keyword).

    So in the example, MrShickadance was telling VB that clsApple Implements the clsFruit interface (interface just means set of functions and subs). If MrShickadance forgot to create one of the subs that clsFruit had in his clsApple class, then VB would have complained.

    So how it helps you is that you can sleep soundly at night knowing that if you decided to change the name of a sub in clsFruit one day or if you added or removed one, VB would make you update all the other classes that Implement clsFruit before you can execute the code.

    I doubt my explanation is enough to get you interested in OO design, but then, VB isn't yet the ideal mechanism to learn OO anyhow

    Cheers
    Paul Lewis



  4. #4
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    I haven't used it much but I've been reading about it a bit lately, but I thought that the functions and subs had to be left unimplemented in the base class! and then implemented in the new classes.

    can you write the line

    WhatAmI = "All Fruits"

    or not?
    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

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