unit GlobalSetting;interface
uses
Windows, Messages, SysUtils, Variants, Classes;
type
TCondition=Record
Field:String;
Operator:String;
Value:String;
end;
TConditions=class(TObject)
private
  FCount:Integer;
  FConditions:Array of TCondition;
  function GetCondition(Index: Integer): TCondition;
  function GetCount:Integer;
protected
public
procedure Add(Condition: TCondition);
property Items[Index:Integer]:TCondition read GetCondition;
property Count:Integer read FCount;
constructor Create;overload;
destructor destroy;override;
end;implementation{ TConditions }function TConditions.GetCondition(Index: Integer): TCondition;
begin
  If Index>FCount-1 then
  begin
    MessageBox(0,'索引值大于最大值','错误',mb_Ok+mb_IconError);
    Exit;
  end;
  if Index<0 then
  begin
    MessageBox(0,'索引值不能小于0','错误',mb_Ok+mb_IconError);
    Exit;
  end;
  Result:=FConditions[Index];
end;procedure TConditions.Add(Condition: TCondition);
begin
  FCount:=Length(FConditions);
  SetLength(FConditions,FCount+1);
  FConditions[FCount]:=Condition;
  FCount:=FCount+1;
end;function TConditions.GetCount: Integer;
begin
  FCount:=Length(FConditions);
  Result:=FCount;
end;destructor TConditions.destroy;
begin
  SetLength(FConditions,0);
  inherited;
end;constructor TConditions.Create;
begin
  FCount:=0;
  SetLength(FConditions,FCount);//运行到这句出错,到底是怎么回事???难道设置数组大小不是这个函数????、
求教了
谢谢
end;

解决方案 »

  1.   

    var a:Tconditions;
    begin
    a.create;//这里就错
    end;
      

  2.   

    它显示的是a.create这行错误,你要在create里面所执行过的代码设置断点,查看到底哪行出的错误?
      

  3.   

    晕,你没uses System吧,老大。
      

  4.   

    在create中设置断点
    好像是错在SETLENGTH上面
    运行到fcount:=0的时候,FCOUNT的值是一个20000多的大整数
    把FCOUNT赋值为0后,再运行SETLENGTH就报错
    我记得以前看过一篇文章,说的好像是动态数组在初始化的时候,长度并不是0,而是一个随机数,不知道是不是这样的原因
    如果是,又该如何解决呢
    我现在有好几个类用了类似的数组处理
    如果解决不了,好多东西就要重来了
    谢谢大家
      

  5.   

    System单元是不需要引用的。调试了一下是你引用的代码有问题,应该是这样:
    var a:Tconditions;
    begin
      a := TConditions.Create;//这里就错
     // 后面释放。
    end;
      

  6.   

    因为你声明了一个变量a,并没有给实例话,就调用a.create,当然要出错。
      

  7.   

    那应该怎么写呢
    难道不是
    a:=TCondition.create?????
      

  8.   

    应该是:a:=TCondition.create,刚才拷贝的 后面注释忘记删除
    看你二楼的引用:
    var a:Tconditions;
    begin
    a.create;//这里就错
    end;
      

  9.   

    哈哈,谢谢,俺刚知道system单元不需要引用,嘿嘿。学习,学习。^_^
      

  10.   

    天啦!!!!!!!!!!
    犯了一个无比愚蠢的错误!!太丢人啦!
    还是
     Dlwxn(蓝天)眼睛尖
    感谢感谢
    同时接受一个教训
    CODE一定要细心
    细心
    再细心!
      

  11.   

    var a:Tconditions;
    begin
      a := Tconditions.create;//这里就错 我靠.还有你那种写法!
    end;
      

  12.   

    constructor TConditions.Create;
    begin
      inherited;  //加上这句,整个世界安静了。
      FCount:=0;
      SetLength(FConditions,FCount);
    end;