Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

excel - Error handling in class returns runtime error 440 "automation error"

I have a class ClsSubject with the let INN property. The property checks wheither the incoming argument is a numeric string and has length of 10 characters. Otherwise it throws an error.

Property Let INN(val As String)
    If IsNumeric(val) And Len(val) = 10 Then
        PINN = val
    Else
        Dim errorMessage As String
        errorMessage = val & " is an Invalid value"
        Err.Raise vbObjectError + 513, "ClsSubject", errorMessage
    End If
End Property

When I try to simulate the error I only get

Runtime error '440': Automation Error.

What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using the code from the OP, the following works. In a module:

Sub TestMe()

    Dim x As New clsSubject
    x.INN = 3276132761#
    Debug.Print x.PINN

End Sub

And this in the class:

Public PINN As String

Property Let INN(val As String)
    If IsNumeric(val) And Len(val) = 10 Then
        PINN = val
    Else
        Dim errorMessage As String
        errorMessage = val & " is an Invalid value"

        Err.Raise vbObjectError + 513, "ClsSubject", errorMessage
    End If
End Property

In order to make the code running, call TestMe().

In the general case, the fields of the class should be private, and should be accessed through public properties. E.g. private PINN as String, but this is only for illustration.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...