Solved

DLL Error Handling

Posted on 2002-05-14
8
145 Views
Last Modified: 2010-05-02
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
0
Comment
Question by:daveamour
  • 4
  • 4
8 Comments
 
LVL 43

Accepted Solution

by:
TimCottee earned 50 total points
ID: 7008412
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
 
LVL 19

Author Comment

by:daveamour
ID: 7008460
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
 
LVL 43

Expert Comment

by:TimCottee
ID: 7008485
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 19

Author Comment

by:daveamour
ID: 7008511
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
 
LVL 19

Author Comment

by:daveamour
ID: 7008522
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
 
LVL 43

Expert Comment

by:TimCottee
ID: 7008587
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
 
LVL 19

Author Comment

by:daveamour
ID: 7008664
Ok thanks very much, you have been very helpful :)

Dave
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 7008669
You are welcome Dave.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question