Results 1 to 5 of 5

Thread: Help: Adding Property to Class that use IMPLEMENTS

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    12

    Question Help: Adding Property to Class that use IMPLEMENTS

    Hi..
    I have problem using class that use IMPLEMENTS. If i create a new class having interface from other class. The problem is I can't add a new public property or method in it. New property that i write didn't recognized by the instance object.

    For example:
    I have Interface Class called IHuman, have 3 Properties:
    - Name
    - DateBirth
    - Sex
    My Code for Interface Class IHuman:
    VB Code:
    1. Public Property Get Name as string
    2. End Property
    3. Public Property Let Name(Byval sData as string)
    4. End Property
    5.  
    6. Public Property Get DateBirth as Date
    7. End Property
    8. Public Property Let DateBirth(Byval dData as Date)
    9. End Property
    10.  
    11. Public Property Get Sex as Integer
    12. End Property
    13. Public Property Let Sex(Byval iData as integer)
    14. End Property
    Then I wrote another class called CEmploye that implements IHuman. Beside Name, DateBirth and Sex, i need new property named Salary.
    My Code for Class CEmploye:
    VB Code:
    1. Implements IHuman
    2.  
    3. Public Property Get Salary as double
    4. End Property
    5. Public Property Let Salary(Byval uData as double)
    6. End Property
    My Form's code:
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim oEmp as IHuman
    3.     Set oEmp as New CEmploye
    4.  
    5.     oEmp.Name = "Mike"
    6.     oEmp.Sex=0
    7.     oEmp.Salary = 1000   '<--error here
    8. End Sub
    Is there any solution(s) to add new method or property to this kind of class ?

    Best Regards,
    - jude -

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Help: Adding Property to Class that use IMPLEMENTS

    You will need to add the Salary Property to the IHuman Interface, rather than to the CEmployee Class.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    12

    Re: Help: Adding Property to Class that use IMPLEMENTS

    Thx...but i wanna get the reusable concept. From IHuman, maybe i'll write CStudent that has no Salary Property, but it has School Property beside Name,Sex and DateBirth...

    - jude7 -

    Quote Originally Posted by DKenny
    You will need to add the Salary Property to the IHuman Interface, rather than to the CEmployee Class.
    Last edited by jude7; Mar 31st, 2006 at 01:44 PM.

  4. #4
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Help: Adding Property to Class that use IMPLEMENTS

    Quote Originally Posted by jude7
    Thx...but i wanna get the reusable concept. From IHuman, maybe i'll write CStudent that has no Salary Property, but it has School Property beside Name,Sex and DateBirth...

    - jude7 -
    The interface -has- to be common to all the shared classes. As you have found out, VB6 doesn't support inheritance. One workaround would be to put all of the properties into the interface class as Declan suggests, but in that class's implementation of the property that is supposed to be 'missing', have it raise a "Method not found" error.
    VB Code:
    1. Public Property Get IHuman_Salary() As Double
    2.  
    3.     Call Err.Raise(438, "CEmploye", "Object doesn't support this property or method.")
    4.  
    5. End Property
    6.  
    7. Public Property Let IHuman_Salary(ByVal uData As Double)
    8.  
    9.     Call Err.Raise(438, "CEmploye", "Object doesn't support this property or method.")
    10.  
    11. End Property
    It might sound kind of dumb to do it this way, but it will give the same result.
    Last edited by Comintern; Mar 31st, 2006 at 01:56 PM. Reason: typo (bad one)

  5. #5
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Help: Adding Property to Class that use IMPLEMENTS

    You cannot reference the different interfaces through the same object variable. Since you declared oEmp as IHuman that interface is all you have access to. Your code should be

    VB Code:
    1. Private Sub Form_Load()
    2.     Dim oEmp as CEmploye
    3.     Dim oHuman as IHuman
    4.  
    5.     Set oEmp as New CEmploye
    6.     Set oHuman = oEmp
    7.  
    8.     oHuman .Name = "Mike"
    9.     oHuman .Sex=0
    10.  
    11.     oEmp.Salary = 1000  
    12.  
    13. End Sub

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