delphi中全局变量能不能用forward提前声明,后面再定义?

解决方案 »

  1.   

    variables declared in interface section are always public by default. You can call these public variables from another units by just adding the unit to uses clause.
      

  2.   

    Sure you can declare the variable in interface section and do assignment in implementation section.
    This don't apply to constants (const). 
      

  3.   

    unit Unit2;interface
    var a:Cardinal;
    implementation
    var a:Cardinal=1;
    end.[DCC Error] Unit2.pas(6): E2004 Identifier redeclared: 'a'我需要编译时初始化,不要运行时的赋值
      

  4.   

    don't declare same variable again in implementation section. Just assign the value to that variable.Remeber var keyword declares the variable.
    interface  var a: Cardinal;implementationbegin
         a := 1;
    end;......
      

  5.   

    I prefer compile-time initialization to run-time assignment.
      

  6.   

    I don't think there exist any way in delphi for such kind of variable forward declaration.Just a suggestion, replace top level variable with method and declare a variable in implementation section (thus local to that unit) and outer units will access this via calling public methode.g
    interface
    ....function A: Cardinal;implementationvar
       _a : 1;function A:Cardinal;
    Result := _a;
    end;
      

  7.   

    this variable is only used locally,and I just want to put the define at the end of the file
      

  8.   

    Looks like you will have to go with run time initialization.you can declare variable in implementation section and have it initialized in initialization section.statements in initialization section are executed at the time when module is loaded,so it is guaranteed that any code in initialization section will be executed before any other code in unit.
      

  9.   

    Delphi不允许这样定义。和C,C++不同