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
LVL 19
daveamourAsked:
Who is Participating?
 
TimCotteeConnect With a Mentor Head of Software ServicesCommented:
It all depends on what you mean by an illegal amount. Based on this sample you are almost certain to get some errors as you pass the value as a long yet assign it to what appears to be an integer.

From your code I would do the following, change the property to make the value passed by value rather than by reference:

Public Property Let BorrowAmount(ByVal temp As Long)

Then make sure that the value that you are passing has the same data type as you are really interested in. Long/Integer etc.

Then add some error handling in the property procedure and possibly add an event so that this can be passed back to the calling application.

Public Event DllError(ByVal Description As String, ByVal Comments As String)

Public Property Let BorrowAmount(ByVal temp As Integer)
   On Error Goto errBorrow
   intBorrowAmount = temp
   Exit Property

errBorrow:
  RaiseEvent DllError("Invalid borrow amount",Cstr(Temp))
End Property
0
 
daveamourAuthor Commented:
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
0
 
TimCotteeHead of Software ServicesCommented:
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.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
daveamourAuthor Commented:
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

0
 
daveamourAuthor Commented:
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
0
 
TimCotteeHead of Software ServicesCommented:
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.
0
 
daveamourAuthor Commented:
Ok thanks very much, you have been very helpful :)

Dave
0
 
TimCotteeHead of Software ServicesCommented:
You are welcome Dave.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.