访问存储在另一个单元Delphi中的数据
在我的程序的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将
Data := TData.Create;
添加到 Unit2 的initialization
部分,或将 TData 更改为记录而不是对象。只要正确初始化,从 Unit1 访问 Unit2 的全局对象并没有本质上的错误。Add
Data := TData.Create;
to Unit2'sinitialization
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.@Hendriksen123,您在使用变量
Data
之前是否初始化它?EAccessViolation
是无效内存访问错误的异常类,通常在您的代码尝试访问尚未创建(初始化)或已销毁的对象时发生。尝试使用
Data := TData.Create;
然后您可以使用
Data
var。@Hendriksen123, do you initialize the variable
Data
before using it? theEAccessViolation
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.那行得通
That works