VB6 使用 Set 关键字生成空对象
VB6 Using Set keyword producing empty object
我想将对象传递给 class 并让该对象保留所有引用,然后需要将该对象传递给表单。尝试我的代码会产生 运行 时间错误 438,"Object doesn't support this property or method".
我不知道我做错了什么,但我需要将对象引用传递给不同的形式并能够从中提取值。
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
然后是测试中的代码 class:
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
测试Class
Option Explicit
Public ScanObj As Scan
Public Name As String
Private Sub Class_Initialize()
Name = "Some Name"
End Sub
扫描Class
Option Explicit
Public Function Get_P() As String
Get_P = "Some String"
End Function
我想将对象传递给 class 并让该对象保留所有引用,然后需要将该对象传递给表单。尝试我的代码会产生 运行 时间错误 438,"Object doesn't support this property or method".
我不知道我做错了什么,但我需要将对象引用传递给不同的形式并能够从中提取值。
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
然后是测试中的代码 class:
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
测试Class
Option Explicit
Public ScanObj As Scan
Public Name As String
Private Sub Class_Initialize()
Name = "Some Name"
End Sub
扫描Class
Option Explicit
Public Function Get_P() As String
Get_P = "Some String"
End Function