Accessing data stored in another unit Delphi

做~自己de王妃 提交于 2019-12-13 01:28:15

问题


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?


回答1:


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.




回答2:


@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.




回答3:


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



来源:https://stackoverflow.com/questions/2630614/accessing-data-stored-in-another-unit-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!