Click to See Complete Forum and Search --> : implements
rhermans
January 26th, 2000, 03:09 AM
I'm trying to get the hang of implements but am running always against the same wall.
I've got a dll that contains some abstract classes that I implement into another
activex dll. I then see in the interfaces in the codewindow and can make the local definition of them.
But now where I loose it.
I try to use this class then in another project and have after refering to this class no problem creating instances of the class
but how do i uses the implemented interface, i don't find it.
Thanks for any help you can send me
Ronny
Chris Eastwood
January 26th, 2000, 03:30 AM
It shouldn't matter where you are implementing the interface from - the standard way of using an 'implements' style object is to refer to it in two ways :
(eg. IPerson.cls) - empty class :
option Explicit
'
private msName() as string
private msEmailAddress as string
'
public property get Name() as string
'
End property
'
public property let Name(byval sName as string)
'
End property
'
public property get Email() as string
'
End property
'
public property let Email(byval sEmail as string)
'
End property
Now add a new class module called 'cEmployee' that implements this interface :
option Explicit
'
Implements IPerson
'
' IPerson Members
'
private msEmail as string
private msName as string
'
' cEmployee Members
'
private msDepartment as string
'
public property get Department() as string
Department = msDepartment
End property
'
public property let Department(byval sDepartment as string)
msDepartment = sDepartment
End property
'
private property let IPerson_Email(byval RHS as string)
msEmail = RHS
End property
'
private property get IPerson_Email() as string
IPerson_Email = msEmail
End property
'
private property let IPerson_Name(byval RHS as string)
msName = RHS
End property
'
private property get IPerson_Name() as string
IPerson_Name = msName
End property
Now this 'cEmployee' class implements the IPerson interface. That means that cEmployee has the same properties / methods as 'IPerson' but also has some of its own (in this example, only a 'Department' property).
It's important to remember that when you define an object as type 'cEmployee' it can only access those properties available to it - ie. not the ones implemented by IPerson.
For example, here's how you'd create a new 'cEmployee' object :
Dim oPerson as IPerson
Dim oEmployee as cEmployee
'
set oPerson = new cEmployee
set oEmployee = oPerson
'
oPerson.Name = "Chris Eastwood"
oPerson.Email = "chris.eastwood@codeguru.com"
oEmployee.Department = "Somewhere"
'
In the above example, both the oPerson and oEmployee objects are referencing the same object - they're just using the different interfaces.
You could expand on this example by having another class that implements the 'IPerson' interface (eg. 'cSlaveWorker') - this could then lead to code such as :
public Sub SendAnEmail (oPerson as IPerson)
'
With ctlSomeMAPITypeControlOrSimilar
.SendEmailTo oPerson.Email, "Have a nice Day"
End With
'
End Sub
- this routine (completely made up ;-) ) could then be used to send emails to any 'Person' type object that implements the IPerson interface - meaning a lot less coding.
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb
rhermans
January 26th, 2000, 05:55 AM
Just one more stupid question --- I reference the Class IPerson.cls that has been implemented also in the Class CEmployee. But is it normal that I also have to reference the Class IPerson.cls in the module/form/... that uses the CEmployee class.
and not only the CEmployee class.
thanks in advance
Ronny
Chris Eastwood
January 26th, 2000, 07:47 AM
I think I understand your question....
An interface class can be stored in a separate DLL or in a DLL with the other classes that implement it's interface (it's all down to design). Obviously if you have them in separate DLL's, you'll need to reference both from your main project.
You don't have to use the IPerson interface into the cEmployee class at all - interface based programming just allows you to create a common method of programming objects.
For example, say your IPerson interface has a couple of extra methods such as 'Load', 'Save' and 'Delete'. You can then have other classes implement this interface and they can all be called from a common routine such as :
public Sub DoSomething(oPerson as IPerson, iAction as Integer)
Select Case iAction
Case 0 ' Load
oPerson.Load
Case 1 ' Save
oPerson.Save
Case 2 ' Delete
oPerson.Delete
End Select
End Sub
The beauty of this is that each object that implements the IPerson interface can handle the method in different ways. The 'cEmployee' Save method might write it's data away to an EMPLOYEE table in a database, while the 'cSlaveWorker' might write the data to a SLAVELABOUR table.
For a full example, I'd recommend you take a look at the source code at http://codeguru.developer.com/vb/articles/1634.shtml.
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.