类模块中 Public Sub ShowCpuInfo() On Error GoTo errHandle Dim i As Integer If UBound(MySys) = -1 Then Exit Sub For i = 0 To UBound(MySys) MsgBox MySys(i).CPU Next errHandle: If Err.Number = 9 Then MsgBox "数组为空" End If End Sub模块中 Private Type SystemInfo CPU As Variant Memory As Long VideoColors As Integer Cost As Currency PurchaseDate As Variant End TypePublic MySys() As SystemInfo窗体中 Dim MyClass As Class1 Private Sub Command1_Click() MyClass.ShowCpuInfo End SubPrivate Sub Form_Load() Set MyClass = New Class1 initMySys ’使用此句则正常通过,不使用则捕捉到数组为空 End Sub sub InitMySys() ReDim Preserve MySys(0) MySys(0).CPU = "p3" ReDim Preserve MySys(1) MySys(1).CPU = "p4" end sub
if ubound(lhp)<>0 then
elseend if
Public Sub ShowCpuInfo()
On Error GoTo errHandle
Dim i As Integer
If UBound(MySys) = -1 Then Exit Sub
For i = 0 To UBound(MySys)
MsgBox MySys(i).CPU
Next
errHandle:
If Err.Number = 9 Then
MsgBox "数组为空"
End If
End Sub模块中
Private Type SystemInfo
CPU As Variant
Memory As Long
VideoColors As Integer
Cost As Currency
PurchaseDate As Variant
End TypePublic MySys() As SystemInfo窗体中
Dim MyClass As Class1
Private Sub Command1_Click()
MyClass.ShowCpuInfo
End SubPrivate Sub Form_Load()
Set MyClass = New Class1
initMySys ’使用此句则正常通过,不使用则捕捉到数组为空
End Sub
sub InitMySys()
ReDim Preserve MySys(0)
MySys(0).CPU = "p3"
ReDim Preserve MySys(1)
MySys(1).CPU = "p4"
end sub