无法访问的值导致访问冲突
Inaccessible value causing Access violation
我有一个制作自行车的程序(TObject)
调用我的 Create
方法时,出现访问冲突错误 00453359
和地址写入 00000004
。
constructor MyBike.Create(iPrice, iStroke, iYear, iCC: Integer; sName,
sModel: string);
begin
fCC := iCC; // <- Here is the error
fPrice := iPrice;
fStroke := iStroke;
fYear := iYear;
fName := sName;
fModel := sModel;
当我看那行时,它说它是一个 inaccessible value
,至于那里的所有变量。
这是我剩下的class:
type
MyBike = class(TObject)
private
fCC, fStroke, fYear, fPrice: Integer; //I will at a later stage use fPrice as a currency
fName, fModel: string;
public
constructor Create(iPrice, iStroke, iYear, iCC: Integer; sName, sModel:
string);
function GetValues: string;
end;
implementation
{ MyBike }
constructor MyBike.Create(iPrice, iStroke, iYear, iCC: Integer; sName,
sModel: string);
begin
fCC := iCC;
fPrice := iPrice;
fStroke := iStroke;
fYear := iYear;
fName := sName;
fModel := sModel;
end;
和我的主要单位:
private
{ Private declarations }
NewBike : MyBike;
public
{ Public declarations }
end;
var
Form1: TForm1;
redtSavedObject: TRichEdit;
btnClearSavedObject: TButton;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
btnSaveToText.Enabled := False;
btnSavetodata.Enabled := False;
end;
procedure TForm1.btnSaveasObjectClick(Sender: TObject);
var
Price, Year, CC, Stroke : Integer;
Name, Model : String;
begin
Price := StrToInt(edtPrice.Text); //All of these values are fine
Year := StrToInt(edtYear.Text);
CC := StrToInt(edtCC.Text);
Stroke := StrToInt(edtStroke.Text);
Name := edtName.Text;
Model := edtModel.Text;
NewBike.Create(Price, Stroke, Year, CC, Name, Model);
我查看了这个 post:Delphi strange inaccessible value (acess violation) o.O 并说我必须将项目设置编辑为:
调试信息:开启
本地符号:开启
优化:关闭。
我做了重建,仍然没有变化。我已经重启我的电脑了,但无济于事
改变
NewBike.Create(Price, Stroke, Year, CC, Name, Model);
到
NewBike := MyBike.Create(Price, Stroke, Year, CC, Name, Model);
这是创建 Class 新实例的正确方法。
当您创建 class 的新实例时,您会在 Class (MyBike
) 上调用构造函数并将其返回值分配给变量 NewBike := MyBike.Create(
。 ..);`
在每个对象(class 的实例)中,您都有一个名为 Self
的隐藏参数,有关 Delphi Basics 的更多信息。你的问题是你没有创建 class 的新实例,因此你的自变量是 nil。
我有一个制作自行车的程序(TObject)
调用我的 Create
方法时,出现访问冲突错误 00453359
和地址写入 00000004
。
constructor MyBike.Create(iPrice, iStroke, iYear, iCC: Integer; sName,
sModel: string);
begin
fCC := iCC; // <- Here is the error
fPrice := iPrice;
fStroke := iStroke;
fYear := iYear;
fName := sName;
fModel := sModel;
当我看那行时,它说它是一个 inaccessible value
,至于那里的所有变量。
这是我剩下的class:
type
MyBike = class(TObject)
private
fCC, fStroke, fYear, fPrice: Integer; //I will at a later stage use fPrice as a currency
fName, fModel: string;
public
constructor Create(iPrice, iStroke, iYear, iCC: Integer; sName, sModel:
string);
function GetValues: string;
end;
implementation
{ MyBike }
constructor MyBike.Create(iPrice, iStroke, iYear, iCC: Integer; sName,
sModel: string);
begin
fCC := iCC;
fPrice := iPrice;
fStroke := iStroke;
fYear := iYear;
fName := sName;
fModel := sModel;
end;
和我的主要单位:
private
{ Private declarations }
NewBike : MyBike;
public
{ Public declarations }
end;
var
Form1: TForm1;
redtSavedObject: TRichEdit;
btnClearSavedObject: TButton;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
btnSaveToText.Enabled := False;
btnSavetodata.Enabled := False;
end;
procedure TForm1.btnSaveasObjectClick(Sender: TObject);
var
Price, Year, CC, Stroke : Integer;
Name, Model : String;
begin
Price := StrToInt(edtPrice.Text); //All of these values are fine
Year := StrToInt(edtYear.Text);
CC := StrToInt(edtCC.Text);
Stroke := StrToInt(edtStroke.Text);
Name := edtName.Text;
Model := edtModel.Text;
NewBike.Create(Price, Stroke, Year, CC, Name, Model);
我查看了这个 post:Delphi strange inaccessible value (acess violation) o.O 并说我必须将项目设置编辑为:
调试信息:开启
本地符号:开启
优化:关闭。
我做了重建,仍然没有变化。我已经重启我的电脑了,但无济于事
改变
NewBike.Create(Price, Stroke, Year, CC, Name, Model);
到
NewBike := MyBike.Create(Price, Stroke, Year, CC, Name, Model);
这是创建 Class 新实例的正确方法。
当您创建 class 的新实例时,您会在 Class (MyBike
) 上调用构造函数并将其返回值分配给变量 NewBike := MyBike.Create(
。 ..);`
在每个对象(class 的实例)中,您都有一个名为 Self
的隐藏参数,有关 Delphi Basics 的更多信息。你的问题是你没有创建 class 的新实例,因此你的自变量是 nil。