在delphi单元中,我有一个名为“ Context”的全局记录:
interface
type
TContext = record
...
end;
var
context: TContext;
在本单元中,我还会有一个初始化过程,需要一个上下文:
interface
procedure Init(AContext: TContext);
在初始化过程中,我尝试将给定的上下文分配给全局上下文:
implementation
procedure Init(AContext: TContext);
begin
context := AContext;
end;
由于某些原因,分配后全局上下文仍为空。这是为什么?
在过程内部声明一个局部变量,并对其进行分配按预期工作。
我应该提到的是,该单元位于dll中,并且从exe调用了初始化过程。
声明全局记录或声明多个全局字符串没有区别。分配的值将丢失。
问候,
-醋
最佳答案
我想您必须显示更多代码。用
unit Unit1;
interface
type
TContext = record
dummy: Integer;
end;
var
context: TContext;
procedure Init(AContext: TContext);
implementation
procedure Init(AContext: TContext);
begin
context := AContext;
end;
end.
和
program Project1;
{$APPTYPE CONSOLE}
uses
Unit1 in 'Unit1.pas';
procedure Test;
var
c: TContext;
begin
c.dummy := 666;
Init(c);
end;
begin
Test;
end.
我得到了预期的结果,即
c
和Unit1.context
在Init(c);
中执行Test
后都包含666。
关于delphi - Delphi:分配给全局记录-全局记录不变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3201849/