所以,我有这个类,我调用了几个检查数据的类。他们返回一个名为Failcase的错误类。现在,当我第一次将错误设置为true时,出现错误。
错误说明:
无效使用财产。
Private Sub btnImport_Click()
Dim fail As Failcase
Set fail.Success = True '<---- This is where the error occures
Set fail = ImportCheckSpec(Me.txtImportSpec)
If fail.Success Then
MsgBox "Error " + CStr(fail.Code) + ": " + fail.Message, vbCritical, "Error"
Exit Sub
End If
Set fail = ImportCheckDate(Me.txtDateTime)
If fail.Success Then
MsgBox "Error " + CStr(fail.Code) + ": " + fail.Message, vbCritical, "Error"
Exit Sub
Else
MsgBox "Success"
End If
End Sub
Failcase类看起来像这样:
Option Compare Database
Option Explicit
Public Success As Boolean
Public Code As Integer
Public Message As String
我用:
答案 0 :(得分:2)
您正在使用OOP而不创建类Failcase
的新对象。在模块中尝试这个:
Option Explicit
Public Sub TestMe()
Dim fail As New failcase
fail.Success = True
Debug.Print fail.Success
End Sub
在课堂上:
Option Explicit
Private m_bSuccess As Boolean
Public Property Get Success() As Boolean
Success = m_bSuccess
End Property
Public Property Let Success(ByVal bNewValue As Boolean)
m_bSuccess = bNewValue
End Property
因此,您将实现封装。有了它,您可以为访问您的媒体资源设置更多规则 - https://www.google.com/search?q=encapsulation+oop&oq=encapsulation+oop&aqs=chrome..69i57j0l5.3599j0j7&sourceid=chrome&ie=UTF-8
上面的代码是early binding的示例。这是另一个,后期绑定的例子,做同样的事情:
Public Sub TestLateBinding()
Dim fail As Object
Set fail = New failcase
fail.Success = True
Debug.Print fail.Success
End Sub
早期和晚期绑定具有不同的优点和缺点。
答案 1 :(得分:1)
就像这样。
Dim fail As New Failcase
fail.Success = True