【发布时间】:2014-02-16 18:45:28
【问题描述】:
我正在尝试在我的程序中使用一个类。
TStack = Class
Public
constructor Create; Overload;
Procedure Add(Frm:TForm);
Procedure Remove();
Procedure Do_Close;
Private
List : Array[1..Max_Forms] of Rec;
Count : Byte;
End;
构造函数:
constructor TStack.Create;
begin
Self.Count := 0;
end;
Procedure TStack.Add(Frm:TForm);
begin
Inc(Self.Count);
List[Count].Form := @Frm;
List[Count].Width := Frm.Width;
List[Count].Height := Frm.Height;
List[Count].left := Frm.Left;
List[Count].Top := Frm.Top;
end;
我无法更改 Count 变量的值!它会导致运行时错误:访问冲突....地址写入 000001E4
有什么问题?!
更多信息:
我正在尝试将指向每个表单的指针存储在这样的结构中:
Rec = Record
Form : ^TForm;
Maximized : Boolean;
Width,
Height,
left,
Top : Integer;
End;
然后
Procedure TStack.Do_Close;
var
i : integer;
MyForm : TForm;
begin
i := .....some code here.......;
MyForm := @List[i].Form;
ShowMessage('I will close '+MyForm.Caption);
MyForm.Close;
end;
并像这样调用构造函数来初始化'Count':
Stack.Create;
【问题讨论】:
-
哪一行崩溃,构造函数中的还是Add中的?
-
很多问题。可能伤害您的是您没有正确创建对象。您没有显示该代码。请做。使用整数来计数,而不是字节。不要使用静态数组。使用动态数组或
TList<T>。从0开始索引数组。不要取局部变量的地址。 -
是的,
Stack.create不起作用但stack=Tstack.create解决了一些问题(为什么?!)!为什么我不应该使用静态数组?! “不要取局部变量的地址”是什么意思!谢谢你们:) -
OT:有一个现成的
TObjectStack类(根据您显示的代码)您可以为您的任务实现。 -
@TLama 我认为
Rec是一个记录,一个值类型,因此不适合TObjectStack。此外,TObjectStack是遗留的,因为我们有泛型。
标签: class delphi constructor delphi-xe4