主题:[讨论]可以运行但似乎编译不了的代码
最近遇到一个比较奇怪的问题,一个程序在设计时可以运行,但居然编译不了.查来查去凭经验认为是其中的访问多层次的Type嵌套结构时出的问题,于是就写了如下的示例来验证自己的想法.果然.
Option Explicit
Private Type A
arr() As Integer
arrCount As Integer
End Type
Private Type B
arrA() As A
aCount As Integer
End Type
Private Type C
arrB() As B
bCount As Integer
End Type
Private Type D
arrC() As C
cCount As Integer
End Type
Private Type E
arrD() As D
dCount As Integer
End Type
Private Type F
arrE() As E
eCount As Integer
End Type
Private Sub Command1_Click()
Dim i As Integer, j As Integer, k As Integer, m As Integer, n As Integer, x As Integer
Dim y As Integer
Dim myF As F
Dim tmpE As E
Dim tmpD As D
Dim tmpC As C
Dim tmpB As B
Dim tmpA As A
myF.eCount = 5
ReDim myF.arrE(4) As E
For i = 0 To 4
tmpE.dCount = 5
ReDim tmpE.arrD(4) As D
For j = 0 To 4
tmpD.cCount = 5
ReDim tmpD.arrC(4) As C
For k = 0 To 4
tmpC.bCount = 5
ReDim tmpC.arrB(4) As B
For m = 0 To 4
tmpB.aCount = 5
ReDim tmpB.arrA(4) As A
For n = 0 To 4
tmpA.arrCount = 5
ReDim tmpA.arr(4) As Integer
For x = 0 To 4
tmpA.arr(x) = Rnd() * 100
'Debug.Print tmpA.arr(x)
Randomize
Next
tmpB.arrA(n) = tmpA
Next
tmpC.arrB(m) = tmpB
Next
tmpD.arrC(k) = tmpC
Next
tmpE.arrD(j) = tmpD
Next
myF.arrE(i) = tmpE
Next
'debug语句在编译时忽略
'Debug.Print myF.arrE(0).arrD(0).arrC(0).arrB(0).arrA(0).arr(0)
'当以下一行有效时,不能编译
y = myF.arrE(0).arrD(0).arrC(0).arrB(0).arrA(0).arr(0)
'以下为变通方式
'为了能够编译得到y值,本想直接一层一层地访问下去,竟然行不通.只好分两次访问,中间提供几个"歇气"的变量."饭要一口一口地吃,路要一步一步地走"乎?
'tmpC = myF.arrE(0).arrD(0).arrC(0)
'y = tmpC.arrB(0).arrA(0).arr(0)
Debug.Print y
End Sub
先运行下,OK吧;再生成exe文件...,在我的电脑上无法编译,"卡"住了,无响应.我没耐心,只好把vb6 IDE关了.
Option Explicit
Private Type A
arr() As Integer
arrCount As Integer
End Type
Private Type B
arrA() As A
aCount As Integer
End Type
Private Type C
arrB() As B
bCount As Integer
End Type
Private Type D
arrC() As C
cCount As Integer
End Type
Private Type E
arrD() As D
dCount As Integer
End Type
Private Type F
arrE() As E
eCount As Integer
End Type
Private Sub Command1_Click()
Dim i As Integer, j As Integer, k As Integer, m As Integer, n As Integer, x As Integer
Dim y As Integer
Dim myF As F
Dim tmpE As E
Dim tmpD As D
Dim tmpC As C
Dim tmpB As B
Dim tmpA As A
myF.eCount = 5
ReDim myF.arrE(4) As E
For i = 0 To 4
tmpE.dCount = 5
ReDim tmpE.arrD(4) As D
For j = 0 To 4
tmpD.cCount = 5
ReDim tmpD.arrC(4) As C
For k = 0 To 4
tmpC.bCount = 5
ReDim tmpC.arrB(4) As B
For m = 0 To 4
tmpB.aCount = 5
ReDim tmpB.arrA(4) As A
For n = 0 To 4
tmpA.arrCount = 5
ReDim tmpA.arr(4) As Integer
For x = 0 To 4
tmpA.arr(x) = Rnd() * 100
'Debug.Print tmpA.arr(x)
Randomize
Next
tmpB.arrA(n) = tmpA
Next
tmpC.arrB(m) = tmpB
Next
tmpD.arrC(k) = tmpC
Next
tmpE.arrD(j) = tmpD
Next
myF.arrE(i) = tmpE
Next
'debug语句在编译时忽略
'Debug.Print myF.arrE(0).arrD(0).arrC(0).arrB(0).arrA(0).arr(0)
'当以下一行有效时,不能编译
y = myF.arrE(0).arrD(0).arrC(0).arrB(0).arrA(0).arr(0)
'以下为变通方式
'为了能够编译得到y值,本想直接一层一层地访问下去,竟然行不通.只好分两次访问,中间提供几个"歇气"的变量."饭要一口一口地吃,路要一步一步地走"乎?
'tmpC = myF.arrE(0).arrD(0).arrC(0)
'y = tmpC.arrB(0).arrA(0).arr(0)
Debug.Print y
End Sub
先运行下,OK吧;再生成exe文件...,在我的电脑上无法编译,"卡"住了,无响应.我没耐心,只好把vb6 IDE关了.