访问存储在另一个单元Delphi中的数据

发布于 2024-08-28 08:06:00 字数 826 浏览 8 评论 0原文

在我的程序的 Unit2 中,我有以下代码:

TValue = Record
  NewValue,
  OldValue,
  SavedValue : Double;
end; 

TData = Class(TObject)
Public
  EconomicGrowth : TValue;
  Inflation : TValue;
  Unemployment : TValue;
  CurrentAccountPosition : TValue;
  AggregateSupply : TValue;
  AggregateDemand : TValue;
  ADGovernmentSpending : TValue;
  ADConsumption : TValue;
  ADInvestment : TValue;
  ADNetExports : TValue;
  OverallTaxation : TValue;
  GovernmentSpending : TValue;
  InterestRates : TValue;
  IncomeTax : TValue;
  Benefits : TValue;
  TrainingEducationSpending : TValue;
End;

然后我在 Var 中声明 Data : TData 。

然而,当我尝试在 Unit1 中执行以下操作时:

  ShowMessage(FloatToStr(Unit2.Data.Inflation.SavedValue));

我收到一条 EAccessViolation 消息。有没有办法从 Unit1 访问存储在“Data”中的数据而不会出现错误?

In Unit2 of my program i have the following code:

TValue = Record
  NewValue,
  OldValue,
  SavedValue : Double;
end; 

TData = Class(TObject)
Public
  EconomicGrowth : TValue;
  Inflation : TValue;
  Unemployment : TValue;
  CurrentAccountPosition : TValue;
  AggregateSupply : TValue;
  AggregateDemand : TValue;
  ADGovernmentSpending : TValue;
  ADConsumption : TValue;
  ADInvestment : TValue;
  ADNetExports : TValue;
  OverallTaxation : TValue;
  GovernmentSpending : TValue;
  InterestRates : TValue;
  IncomeTax : TValue;
  Benefits : TValue;
  TrainingEducationSpending : TValue;
End;

I then declare Data : TData in the Var.

when i try to do the following however in Unit1:

  ShowMessage(FloatToStr(Unit2.Data.Inflation.SavedValue));

I get an EAccessViolation message. Is there any way to access the data stored in 'Data' from Unit1 without getting errors?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一抹淡然 2024-09-04 08:06:00

Data := TData.Create; 添加到 Unit2 的 initialization 部分,或将 TData 更改为记录而不是对象。只要正确初始化,从 Unit1 访问 Unit2 的全局对象并没有本质上的错误。

Add Data := TData.Create; to Unit2's initialization section, or change TData to a record instead of an object. There's nothing inherently wrong with accessing Unit2's global objects from Unit1 as long as they're properly initialized.

千纸鹤带着心事 2024-09-04 08:06:00

@Hendriksen123,您在使用变量 Data 之前是否初始化它? EAccessViolation 是无效内存访问错误的异常类,通常在您的代码尝试访问尚未创建(初始化)或已销毁的对象时发生。

尝试使用 Data := TData.Create;

然后您可以使用 Data var。

@Hendriksen123, do you initialize the variable Data before using it? the EAccessViolation is the exception class for invalid memory access errors, and usually occurs when your code tries to access an object that has not created (initialized) or has already been destroyed.

try using Data := TData.Create;

and then you can use the Data var.

此岸叶落 2024-09-04 08:06:00
unit Unit2;

interface

type
  TValue = Record
    NewValue,
    OldValue,
    SavedValue : Double;
  end;

  TData = Class(TObject)
  Public
    EconomicGrowth : TValue;
    Inflation : TValue;
    Unemployment : TValue;
    CurrentAccountPosition : TValue;
    AggregateSupply : TValue;
    AggregateDemand : TValue;
    ADGovernmentSpending : TValue;
    ADConsumption : TValue;
    ADInvestment : TValue;
    ADNetExports : TValue;
    OverallTaxation : TValue;
    GovernmentSpending : TValue;
    InterestRates : TValue;
    IncomeTax : TValue;
    Benefits : TValue;
    TrainingEducationSpending : TValue;
  End;

procedure InitialiseData (var pData : TData);

implementation

procedure InitialiseData (var pData : TData);
begin
  pData := TData.Create;

  pData.EconomicGrowth.SavedValue := 1.00;

end;

end.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Unit2,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  vData : TData;
begin
  Unit2.InitialiseData(vData);

  ShowMessage(FloatToStr(vData.EconomicGrowth.SavedValue));

end;

end.

那行得通

unit Unit2;

interface

type
  TValue = Record
    NewValue,
    OldValue,
    SavedValue : Double;
  end;

  TData = Class(TObject)
  Public
    EconomicGrowth : TValue;
    Inflation : TValue;
    Unemployment : TValue;
    CurrentAccountPosition : TValue;
    AggregateSupply : TValue;
    AggregateDemand : TValue;
    ADGovernmentSpending : TValue;
    ADConsumption : TValue;
    ADInvestment : TValue;
    ADNetExports : TValue;
    OverallTaxation : TValue;
    GovernmentSpending : TValue;
    InterestRates : TValue;
    IncomeTax : TValue;
    Benefits : TValue;
    TrainingEducationSpending : TValue;
  End;

procedure InitialiseData (var pData : TData);

implementation

procedure InitialiseData (var pData : TData);
begin
  pData := TData.Create;

  pData.EconomicGrowth.SavedValue := 1.00;

end;

end.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Unit2,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  vData : TData;
begin
  Unit2.InitialiseData(vData);

  ShowMessage(FloatToStr(vData.EconomicGrowth.SavedValue));

end;

end.

That works

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文