CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: implements

  1. #1
    Join Date
    Jan 2000
    Location
    Belgium / Europe
    Posts
    8

    implements

    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



  2. #2
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: implements

    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 = "[email protected]"
    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

  3. #3
    Join Date
    Jan 2000
    Location
    Belgium / Europe
    Posts
    8

    Re: implements

    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


  4. #4
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: implements

    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

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

Featured