borderAndreaVB free resources for Visual Basic developersborder

AndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2008 Andrea Tincani
:: How to embed an error code in a standard function return

Author  

Bob Wallace

Language  

VB5, VB6

Operating Systems  

Windows 95, 98, NT, Me and 2k
Module
'I have read many accounts in various books and articles about how it would
'be nice to be able to return an error code with every function return...
'it's fairly easy to achieve this

'Simply create a User-Defined-Type (or class) called tReturn (or whatever) in
'a global module (assuming the use of a UDT).

'---------------------------------------------
Type tReturn
     Value as Variant
     Error as Integer
End Type
'---------------------------------------------
Usage
'Then make each function of type tReturn.

'---------------------------------------------
Public Function MyFunc(arg1, arg2) as tReturn
'---------------------------------------------

'When coding the function body, if an error occurs simply assign a non-zero
'value to the ".Error" item in the return value

'---------------------------------------------
Public Function MyFunc(arg1, arg2) as tReturn
   ...
   MyFunc.Error = 5 'Some Error
          (or)
   MyFunc.Error = Err
   ...
End Function
'---------------------------------------------

'This way, you can always tell if an error has occured by checking the
'existence of an error in the return value

'---------------------------------------------
   Dim MyReturn as tReturn
   ...
   MyReturn = MyFunc(3,8)
   If Not MyReturn.Error Then
      nSomeVariable = MyReturn.Value
   End If
   ...
'---------------------------------------------

'If you create tRetrun as a class (cReturn or whatever), you can make .Value
'as the default property and code it this way:

'---------------------------------------------
   Dim MyReturn as New cReturn
   ...
   MyReturn = MyFunc(3,8)
   If Not MyReturn.Error Then
      nSomeVariable = MyReturn
   End If
   ...
'---------------------------------------------

'Creating a class (which I personally prefer utilizing in my own code) allows
'for fairly sophisticated error handling by embedding functions within the
'class to resolve the errors without needing to stick alot of error-handling
'code all over the place.
:: Navigation

Home

Miscellaneous Tips

Previous Tip

Next Tip

:: Search this site
Google
:: Related Topics
icon 04-04-2007 error in word VBA to automate creation of mailing labels by dgr7
icon 19-04-2006 Console inside App... by RaveMaster
icon 27-10-2004 HELP!! Multiple ping in a thread seem impossible!! by Mig
icon 27-10-2004 Besoin d'aide!!! Des ping dans un thread semble impossibles!! by Mig
icon 27-10-2004 I really need help with thread in VB6. Please help me!!! by Mig
:: Sponsored Links



Partners: Il portale per lui e lei | Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2008 Andrea Tincaniborder