突然发现,dim语句竟然可以写在循环语句中,我晕了 Private Sub Command1_Click() 
    Dim i As Integer 
    For i = 1 To 10 
        Dim j As Integer 
        j = j + 1 
        Debug.Print j 
    Next 
    MsgBox j 
End Sub

解决方案 »

  1.   

    在编译的时候,从代码中先找所有Dim,然后再编译其他语句。
    实际上并没有执行10次Dim。
      

  2.   

    VB的帮助中提到可以任何地方使用dim,而且并dim不是可执行语句。
    不知道记错了没有?
      

  3.   


    给C#程序看看(算不得新鲜,但要知道不同的语言,结果并不一样):using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                int i = 1;
                for (; i <= 10; i++)
                {
                    int j = 0;
                    j++;
                    System.Diagnostics.Debug.Write(j + "\n");
                }           }
        }
    }
    //最后结果j=1,而不是10.