Results 1 to 8 of 8

Thread: Errors generated when using get./let in a class

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    Gloucestershire, England
    Posts
    301

    Unhappy

    How do I respond to an error in my calling module when the error was generated with my object?

    This is what I have:

    Calling Module

    Code:
    Private sp As SwitchPack_Class
    
    Public Sub Cal_switchpack(ByVal sp_config As Byte)
    
    On Error GoTo errhandler
    '... code
    Set sp = New SwitchPack_Class
    sp.IR = 512
    '... other code
    exit sub
    
    errhandler:
    
    msgbox "Handled error" ....
    
    End sub
    Class

    Code:
    Public Property Get IR() As Integer
    
      On Error GoTo errhandler
          IR = GetButton(12, RAW)
      Exit Property
    
    errhandler:
      err.Raise err.Number, err.Source, err.Description ' pass error back to calling module
    
    End Property
    
    Private Function GetButton(ByVal button, ByVal mode As Byte) As Integer
    '... other code....
    err.Raise 40001, "Switch Pack Object", "Timeout reading SwitchPack button"
    '... more code
    end function
    At the moment the error gets passed back to the property get function but I cant seem to pass it back to the Cal_Swithpack module. I dont want to use a
    Code:
     if abort=false then sp.ir=12
    as this would make my nice neat function into a massive messy one. Any ideas?

    Cheers


  2. #2
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    If you put the Class in a seperate project then you can trap the errors in the calling module, choose AddProject from the file menu and make it an activeX dll, then add it as a refrence to your original project.

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    I don't know if this helps you or not, but I think what I use for error hadling in a Class is pretty standard.
    Code:
    Public Property Get IsReplacementCPPM() As Boolean
    
        On Error GoTo ErrorRoutine
    
        IsReplacementCPPM = m_bIsReplacementCPPM
       
        Exit Property
        
    ErrorRoutine:
    
        ClassError "IsReplacementCPPM Property Get"
        
    End Property
    
    
    Private Sub ClassError(sRoutine As String)
    
        Dim lError As Long
        
        lError = Err.Number
        
        Err.Clear
        'Send the error back to the main program
        Err.Raise lError, TypeName(Me) & "::" & sRoutine
    
    End Sub

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    Gloucestershire, England
    Posts
    301
    Martin, I can get this to work but it does`nt use my module (calling function handler) it pops up the VB msgbox. Anyideas?


  5. #5

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    VB always breaks on errors in class modules by default. To change this behaviour click on Tools|Options and the General tab. In the Error Trapping frame click the "Break on Unhandled Errors" options.

    Best regards

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    Gloucestershire, England
    Posts
    301
    I dont want to directly call the module handling function. I want to be invoked by the error eg.

    Code:
    Private sub MyModule()
    '...code
    On error goto errhandler
    '...code
    sp.ir=12 ' call to my object
    
    exit sub
    
    errhandler:
    
    Msgbox "Something funny is going on in the Switch Pack object"
    
    End Sub
    I want it to work this way because I am making lots of calls to my object and dont want to have to check for an error every time I use a get or let property.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    Gloucestershire, England
    Posts
    301

    Talking

    Ta chaps, fixed it now (dam menu options!)

    Cheers

    Rick

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width