本文介绍了尝试访问阵列时不断出现访问冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对delphi很陌生,当我尝试访问数组"nieuwButtons"时,我一直遇到访问冲突.有人知道我在做什么错吗?
I'm rather new to delp and I keep getting access violations when I try to access my array "nieuwButtons". Does anyone have any idea what I'm doing wrong?
unit UGeneral;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.DBGrids, SMDBGrid,
KJSMDBGrid, Vcl.ExtCtrls, KJPanel, Vcl.StdCtrls, Vcl.Mask, Vcl.DBCtrls,
Data.DB;
type
TGeneral = class(TObject)
private
{ Private declarations }
public
nieuwButtons: array of TButton;
nieuwButtonsDataSource: array of TDataSource;
procedure listEdits(x, y: Integer; owner: TComponent; parent: TWinControl;
source: TDataSource);
procedure nieuwClick(Sender: TObject);
{ Public declarations }
end;
var
General: TGeneral;
implementation
procedure TGeneral.nieuwClick(Sender: TObject);
var
i: Integer;
begin
for i := 0 to Length(nieuwButtons) - 1 do
begin
if (nieuwButtons[i] = Sender) then
begin
nieuwButtonsDataSource[i].DataSet.Insert;
end;
end;
end;
procedure TGeneral.listEdits(x, y: Integer; owner: TComponent;
parent: TWinControl; source: TDataSource);
var
i: Integer;
edit: TDBEdit;
_label: TLabel;
biggestWidth: Integer;
button: TButton;
edits: array of TDBEdit;
index: Integer;
begin
if nieuwButtons <> nil then //I get an access violation here
begin
SetLength(General.nieuwButtons, 0);
SetLength(nieuwButtonsDataSource, 0);
end;
index := Length(nieuwButtons);
SetLength(nieuwButtons, index + 1);
SetLength(nieuwButtonsDataSource, index + 1);
button := TButton.Create(owner);
button.parent := parent;
button.Top := y;
button.Left := x;
button.Caption := 'Nieuw';
button.OnClick := nieuwClick;
nieuwButtons[index] := button;
nieuwButtonsDataSource[index] := source;
biggestWidth := 0;
SetLength(edits, source.DataSet.Fields.Count);
edit := TDBEdit.Create(owner);
for i := 0 to source.DataSet.Fields.Count - 1 do
begin
_label := TLabel.Create(owner);
_label.parent := parent;
_label.Caption := source.DataSet.Fields[i].FieldName;
_label.Top := ((i * 22) + 30 + y); // 22 = standaard(?) hoogte van edittext
_label.Left := x;
_label.Show;
if _label.Width > biggestWidth then
begin
biggestWidth := _label.Width;
end;
end;
i := 0;
for i := 0 to source.DataSet.Fields.Count - 1 do
begin
edit := TDBEdit.Create(owner);
edit.parent := parent;
edit.DataField := source.DataSet.Fields[i].FieldName;
edit.DataSource := source;
edit.Top := ((i * edit.Height) + 30 + y);
edit.Left := biggestWidth + 30;
edits[i] := edit;
edit.Show;
end;
end;
end.
推荐答案
该行代码引发异常的唯一方法是 TGeneral
的实例无效.
The only way for that line of code to raise an exception is if the instance of TGeneral
is not valid.
该错误将在实例化该类的代码中找到.此错误的常见形式是:
The error will be found in the code that instantiates the class. Common forms of this error are:
- 完全忘记实例化对象.
- 通过在未初始化的实例而非类型上调用构造函数来错误地实例化对象.
为说明后面的错误,它看起来像这样:
To illustrate the latter fault, it looks like this:
var
General: TGeneral;
....
General.Create; // sometimes this way
General := General.Create; // or sometimes this way
但是正确的方法是这样的:
But the correct way is like this:
General := TGeneral.Create;
这篇关于尝试访问阵列时不断出现访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!