|
-
Mar 31st, 2006, 12:20 PM
#1
Thread Starter
New Member
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:
Public Property Get Name as string
End Property
Public Property Let Name(Byval sData as string)
End Property
Public Property Get DateBirth as Date
End Property
Public Property Let DateBirth(Byval dData as Date)
End Property
Public Property Get Sex as Integer
End Property
Public Property Let Sex(Byval iData as integer)
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:
Implements IHuman
Public Property Get Salary as double
End Property
Public Property Let Salary(Byval uData as double)
End Property
My Form's code:
VB Code:
Private Sub Form_Load()
Dim oEmp as IHuman
Set oEmp as New CEmploye
oEmp.Name = "Mike"
oEmp.Sex=0
oEmp.Salary = 1000 '<--error here
End Sub
Is there any solution(s) to add new method or property to this kind of class ?
Best Regards,
- jude -
-
Mar 31st, 2006, 12:50 PM
#2
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 
-
Mar 31st, 2006, 01:15 PM
#3
Thread Starter
New Member
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 -
 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.
-
Mar 31st, 2006, 01:54 PM
#4
Re: Help: Adding Property to Class that use IMPLEMENTS
 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:
Public Property Get IHuman_Salary() As Double
Call Err.Raise(438, "CEmploye", "Object doesn't support this property or method.")
End Property
Public Property Let IHuman_Salary(ByVal uData As Double)
Call Err.Raise(438, "CEmploye", "Object doesn't support this property or method.")
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)
-
Mar 31st, 2006, 02:46 PM
#5
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:
Private Sub Form_Load()
Dim oEmp as CEmploye
Dim oHuman as IHuman
Set oEmp as New CEmploye
Set oHuman = oEmp
oHuman .Name = "Mike"
oHuman .Sex=0
oEmp.Salary = 1000
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|