Sylvain,
PMFJI. I believe you need to raise an error in the COM component if something is wrong rather than returning .F. (using the Err.Raise method) and this error can then be trapped in the calling program with an On Error statement. I have pasted some sample code below. This is code for VB, can this be done in VFP?
Kim
Component Code
Private mvarSalary As Double
Public Property Let Salary(ByVal dNewSalary As Double)
If dNewSalary < 0 Then
Err.Raise 12345 + vbObjectError, "CEmployee", "Salary must be positive"
Else
mvarSalary = dNewSalary
End If
End Property
Public Property Get Salary() As Double
Salary = mvarSalary
End Property
Client Code
Private Sub SetSalary_Click()
' declare an employee object
Dim empCurrent As CompanyComponent.CEmployee
On Error GoTo HandleError
' create the employee and set the salary property
Set empCurrent = New CompanyComponent.CEmployee
' an error could occur here
empCurrent.Salary = CDbl(txtSalary.Text)
Exit Sub
HandleError:
If Err.Number = 12345 + vbObjectError Then
MsgBox "Salary must be positive.", ,Err.Source
End If
End Sub
>Hi Ed,
>
>I wanted to know if there is another way of checking that the COM did instanciate correctly.
>
>My COM is built in VFP. In it's Init() event, I may return .F. if something is wrong. Like you know, this will prevent the COM from instanciating. In VFP, I would write this code:
>
>
loCOM = NewObject("MyCOM.MyClass")
>IF VarType(loCOM) = "O"
> *-- Use loCOM here
>ELSE
> *-- Display error message
>ENDIF
I was wandering if there is a similar approach in VB. In my test, if my COM return .F. in it's Init() event, VB throw an error on the line:
>
>Set loCOM = New MyCOM.MyClass
>
>Is there another way than using On Error in this case?
>
>TIA
>My guess is that it's how he's handling errors in VB; ordinarily I'd trap the failed instantiation by wrapping the SET in an in-line error handler, but I'm far from a VB expert.