因为StringGrid中输入的都是字符串,我希望把字符串转换为实数,以便进行运算,能把每一列储存为一个实数数组(double),我自己编了一个程序,程序如下:unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, StdCtrls;type
  TForm1 = class(TForm)
    Button1: TButton;
    StringGrid1: TStringGrid;
    Edit1: TEdit;
    Edit2: TEdit;
    procedure Button1Click(Sender: TObject);
    //GetData就是获得第Index列,并把列中的字符串转化为实数
    procedure GetData(mStringGrid:TStringGrid;Index:Integer;var Arr:array of Double);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementation
{$R *.dfm}procedure TForm1.GetData(mStringGrid:TStringGrid;Index:Integer;var Arr:array of Double);
var
  I:Integer;
begin
    For I:=1 to mStringGrid.RowCount-1 do
      if mStringGrid.Cells[Index,I]<>'' then
        arr[i]:=StrToFloat(mStringGrid.Cells[Index,I]);
end;procedure TForm1.Button1Click(Sender: TObject);
var
  arr:array of Double;
begin
  GetData(StringGrid1,1,arr);
  Edit1.Text:=FloatToStr(high(arr));
  Edit2.Text:=FloatToStr(low(arr));
end;
end.为什么这段程序的代码会报错,在arr[i]:=StrToFloat(mStringGrid.Cells[Index,I])行中,我调试了发现
mStringGrid.Cells[Index,I])是有值的,如'1',但是StrToFloat(mStringGrid.Cells[Index,I])这个却是‘不可接受的值’。

解决方案 »

  1.   

    别老检查cell中是否有值,看看你的arr数组是否越界
      

  2.   

    初始化时
    selLength(Arr,mStringGrid.RowCount);
    释放时
    Arr:=null;
      

  3.   

    搞定了,我定义了一个新的类型,然后改为函数,代码如下:type TArrDouble=array of Double;
    …………
    //获取Index列数据的长度
    Function TForm1.StringGrid_RowCount(mStringGrid:TStringGrid;Index:Integer):Integer;
    var
      I:Integer;
    begin
      Result:=0;
      For I:=1 to mStringGrid.RowCount-1 do
        if mStringGrid.Cells[Index,I]<>'' then inc(Result);
    end;
    //把Index列的数据取出来
    Function TForm1.GetData(mStringGrid:TStringGrid;Index:Integer):TArrDouble;
    var
      I,L:Integer;
    begin
      L:=Form1.StringGrid_RowCount(mStringGrid,Index);
      setLength(Result,L);
      For I:=1 to L do
        Result[I-1]:=StrToFloat(mStringGrid.Cells[Index,I]);
    end;谢谢大家啦!
      

  4.   

    type TArrDouble=array of Double;
    1.selLength;
    2.null;