VB:在For循环中声明变量

时间:2011-03-05 06:07:21

标签: vb6 for-loop

我一直试图在For循环中声明变量很长一段时间,而我却找不到方法。
我试图在每次在二维阵列中遇到某个数字时创建一个新图像(平铺)(测量32x16)。我可能需要补充一点,我使用的是Visual Basic 6.

目前我正在使用以下代码:

Option Explicit
Dim wCount As Integer
Dim hCount As Integer
Dim arrTiles(31, 15) As Integer


Private Sub Form_Load()
    For wCount = 0 To 31 Step 1
        For hCount = 0 To 15 Step 1
            ' -Declare variables
            ' -I.E. Dim NAME As Image
        Next
    Next
End Sub

但是,上面的代码(使用Dim tile1 As Image)在尝试访问新添加的图像的某个属性(例如tile1.Width)时给出了错误。

有没有办法在运行时以这种方式声明变量?

此致
- Birjolaxew

1 个答案:

答案 0 :(得分:1)

在尝试访问Image变量的任何属性之前,必须将有效的Image对象分配给Image变量。例如,这很好用:

    For wCount = 0 To 31 Step 1
        For hCount = 0 To 15 Step 1
            ' -Declare variables
            Dim tile1 As Image
            tile1 = Image.FromFile("c:/test.png")
            Dim width = tile1.Width
        Next
    Next
相关问题