procedure TFrmrep_chart.IsRepeat(list:TStringList;str:String);
var
i:integer;
bool:boolean;
begin
bool:=false;
for i:=0 to list.Count-1 do
  begin
   if str=list.Strings[i] then
      bool:=true;
  end;
if bool=false then
  begin
   list.Add(str);
  end;
end;
提示错误: read of address 0000000000

解决方案 »

  1.   

    因为list参数为nilStringList本身就能排除重复项procedure TForm1.Button1Click(Sender: TObject);
    var
      vStringList: TStringList;
    begin
      vStringList := TStringList.Create;
      try
        vStringList.Add('abc');
        vStringList.Sorted := True;
        vStringList.Duplicates := dupIgnore;
        vStringList.Add('abc');
        vStringList.Add('abc');
        ShowMessage(vStringList.Text);
      finally
        vStringList.Free;
      end;
    end;
      

  2.   

    可以简化成这样
    procedure IsRepeat(list: TStrings; str: string);
    begin
      if not Assigned(list) then Exit; // 先判断是否不为nil
      if list.IndexOf(str) < 0 then list.Add(str);
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      IsRepeat(Memo1.Lines, 'abc');
      IsRepeat(Memo1.Lines, 'abc');
      IsRepeat(Memo1.Lines, '123');
    end;