Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

DLL Error Handling

Posted on 2002-05-14
8
147 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

809 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