VB6使用Set关键字产生空对象

问题描述 投票:0回答:1

我想把一个对象传给一个类,并让该对象保留所有引用,然后需要把该对象传给一个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

我所期望的工作方式,是否真的是这样?


EDIT:

我把它简化了

我的第一个表单(Login)是我收集所有数据的地方。

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

然后是测试类的代码

Public ScanObj As Object

Public Sub Test()
  MsgBox ScanObj.Get_P
End Sub
vb6
1个回答
0
投票

回到最初的示例代码,我是这样设置的。

登录表格

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
© www.soinside.com 2019 - 2024. All rights reserved.