Link to home
Start Free TrialLog in
Avatar of daveamour
daveamourFlag for United Kingdom of Great Britain and Northern Ireland

asked on

DLL Error Handling

I have written a DLL and this has some public properties eg

Public Property Let BorrowAmount(temp As Long)
    intBorrowAmount = temp
End Property

If a client uses this DLL and assigns an illegal value to the BorrowAmount, I wish to trap this error and handle it within the DLL rather than the client.  What is the best way to accomplish this?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of daveamour

ASKER

intBorrowAmount is a Long actually.  What I meant by illegal was type mismatches, overflows things like that.  Will your suggestion handle that.  If I understand it, it will yes?

Also is it possible to have the errBorrow:
 RaiseEvent DllError("Invalid borrow amount",Cstr(Temp))

in global scope, or do I need one of these for each property?

Many thanks

Dave
You would declare the event in the declarations section of each class that you want the event to be applicable to. You can then instantiate the object in the calling application using the withevents keyword:

Dim WithEvents myClass As MyDll.MyClass

You then get the events in the VB IDE as you would for any other control or class.

You should use the RaiseEvent line in any procedure code within that class that you want to return this information to the calling application. So in your form or module where you use this class:

Dim WithEvents myClass As MyDll.MyClass

Private Sub Command1_Click()
  Set myClass = New MyDll.MyClass
  With myClass
    .BorrowAmount = 100
  End With
  Set myClass = Nothing
End Sub

Private Sub myClass_DllError(ByVal Description As String, ByVal Comments As String)
  Msgbox Description & vbLF & Comments,vbOkOnly,App.Title
End Sub

If you ensure that you explicitly declare the type of the variable in your procedure declarations:

Public Property Let BorrowAmount(ByVal temp As Integer)

Then VB will ensure that you are only passing variables with the correct datatype to the procedure so that partially eliminates the problem there. The error handling I show will also help trap anything that gets through this. Of course you can make this as complex as you want. You may have an event called BoundaryConditions or something like that which you raise when any of the values that you are using are outside of your predefined ranges. This may not be a real type mismatch error as such but can be a great way of providing feedback to the calling application and an implementation of business rules within your dll.
Sorry, one other thing

The RaiseEvent is a Sub/Function which I must write yes?

If so then what is the command for exiting a DLL, I tried End but that doesn't work.

Many thanks

Dave

And 1 other thing!

My client app in this case is an ASP page, can I use WithEvents in this case?

In ASP my code would be:

Dim myClass
Set myClass = Server.CreateObject("MyDll.MyClass")

so how would I add the WithEvents bit in, if I can do this?

I'll give you the points soon!

Thanks

Dave
Dave,

You don't need to write a procedure for RaiseEvent, just use the statement anywhere in a procedure and provide values for the parameters.

You don't actually explicit exit a dll. You just need to destroy any references to it from the calling application.

Set myClass = Nothing

For example, this causes the calling application to release the reference to the object instantiated from the dll and to release any resources associated with that. It also causes the Class_Terminate event within the dll to be fired to ensure that you have the opportunity to cleanup any objects used by the dll before it terminates.

As for ASP, I don't know is the honest answer, I have never done much work with asp so cannot really help on that bit of it. I am sure it is possible but couldn't tell you how.
Ok thanks very much, you have been very helpful :)

Dave
You are welcome Dave.