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 !! :)
Printable View
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 !! :)
Implements refers to giving multiple classes the same interface. I'm not very good at explaining it so I'll give you an example.
get it?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()
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
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?