我想将一个对象传递给一个类并让该对象保留所有引用,然后需要将该对象传递给 Form。尝试我的代码会产生运行时错误 438,"对象不支持此属性或方法"。
我不知道我做错了什么,但我需要将对象引用传递给不同的形式并能够从中提取值。
Private TestObj As New Test
Private ScanObj As New Scan
Private Sub cmdLogin_Click()
Set TestObj.ScanObj = ScanObj 'ScanObj is the object that holds my scan data
Set frmScreen.TestObj = TestObj 'Set the TestObj to another Form that needs to use these vals
frmScreen.Test 'Expecting to see a MsgBox with one of the set object vals, but get nothing back. Why?
frmScreen.Show
End Sub
我对这应该如何工作的期望实际上是这样工作的吗?
编辑:
我已经简化了它。
我收集所有数据的第一个表单(登录(:
Private TestObj As New Test
Private ScanObj As New Scan
Private Sub cmdLogin_Click()
Set TestObj.ScanObj = ScanObj 'ScanObj is the object that holds my scan data
Set frmScreen.TestObj = TestObj 'Set the TestObj in my Test class
TestObj.Test 'Call the Test class Test routine that should show one of my values, but instead get runtime 438
frmScreen.Show
Unload Me
End Sub
然后是 Test 类中的代码:
Public ScanObj As Object
Public Sub Test()
MsgBox ScanObj.Get_P
End Sub
回到原始示例代码,以下是我的设置方式:
登录表格
Option Explicit
Private TestObj As Test
Private ScanObj As Scan
Private Sub Form_Initialize()
Set TestObj = New Test
Set ScanObj = New Scan
End Sub
Private Sub cmdLogin_Click()
Set TestObj.ScanObj = ScanObj
Set frmScreen.TestObj = TestObj
frmScreen.Test
frmScreen.Show
End Sub
屏幕形式
Option Explicit
Public TestObj As Test
Public Sub Test()
MsgBox "Test Name is: " & TestObj.Name
MsgBox "Scanner Name is: " & TestObj.ScanObj.Get_P
End Sub
测试类
Option Explicit
Public ScanObj As Scan
Public Name As String
Private Sub Class_Initialize()
Name = "Some Name"
End Sub
扫描类
Option Explicit
Public Function Get_P() As String
Get_P = "Some String"
End Function