首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

delphi2010结构体泛型list怎么在传地址过程中正确的被list添加

2013-07-04 
delphi2010结构体泛型list如何在传地址过程中正确的被list添加unit Unit1interfaceusesWindows, Messages

delphi2010结构体泛型list如何在传地址过程中正确的被list添加

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, RzLstBox;

type
  TForm1 = class(TForm)
    btn1: TButton;
    mmo1: TMemo;
    lst2: TRzListBox;
    btn2: TButton;
    btn3: TButton;
    mmo2: TMemo;
    lbl1: TLabel;
    txt1: TStaticText;
    procedure btn1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btn3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

type
  pGamelist= ^TGamelist;//这里定义了指针
  TGamelist =packed record
  sky:Integer;
  X: Single;
  Y: Single;
  Z: Single;
  get:Boolean;
  pGamelist:Pointer; //把指针放在结构体里面 不知道是不是可以正常使用
 end;



var
  Form1: TForm1;
  cslist:TStringlist;
  mslist:TStringlist;
implementation

{$R *.dfm}
 uses Generics.Collections;
 var
 List: TList<TGamelist>;//这里是定义的泛型数组

function add(mGamelist:pGamelist):Boolean;
begin
  mGamelist.sky:=mGamelist.sky+9;
  if mGamelist.sky>10 then
    begin
      result:=true;
    end;
    if mGamelist.sky<10 then
    begin
      result:=true;
    end;
   mGamelist.X:=mGamelist.X+9;
   mGamelist.Y:=mGamelist.Y+9;
   mGamelist.Z:=mGamelist.Z+9;
   list.Add(mGamelist); //这里提示出错 ,但是希望不出错原因估计是泛型数组不支持这种操作吧
   ///////mGamelist其实是一个指针 可以怎么才可以转换为可以让泛型数组可以接受的形式呢?/////
   //////////////////貌似怎么写都不可以。。求写过的人或者知道的人 帮忙看下/////
end;
procedure TForm1.btn1Click(Sender: TObject);
var


 ceshi:TGamelist;
 ceshitwo:TGamelist;
 number:Integer;
begin
     ceshi.sky:=1;
     ceshi.X:=100.24;
     ceshi.Y:=55.24;
     ceshi.Z:=0.22;
     ceshi.get:=True;
     add(@ceshi); //调用自己定义的函数,这样也可以
     form1.txt1.Caption:='SKY:'+inttostr(ceshi.sky)+'X:'+FloatToStr(ceshi.X)+'Y:'+FloatToStr(ceshi.Y);
     //number:=list.Add(ceshi);
     number:=list.count;
     if number<>0 then
      begin
      Self.btn1.Caption:= IntToStr(number);
     ceshi.sky:=55;
     ceshi.X:=10.24;
     ceshi.Y:=5.64;
     ceshi.Z:=7.82;
     ceshi.get:=false;
     number:=list.Add(ceshi);
    // add(@list[0]); //写法错误
     add(list[0].pGamelist); //这样也不知道能不能正确添加。

     ceshi.sky:=21;
     ceshi.X:=14.24;
     ceshi.Y:=5.64;
     ceshi.Z:=7.82;
     ceshi.get:=false;
     number:=list.Add(ceshi);
     ceshi.sky:=71;
     ceshi.X:=4.24;
     ceshi.Y:=53.64;
     ceshi.Z:=47.82;
     ceshi.get:=false;
     number:=list.Add(ceshi);
     ceshi.sky:=7;
     ceshi.X:=4.24;
     ceshi.Y:=53.64;
     ceshi.Z:=47.82;
     ceshi.get:=false;
     number:=list.Add(ceshi);
     ceshi.sky:=5;
     ceshi.X:=4.24;
     ceshi.Y:=53.64;
     ceshi.Z:=47.82;
     ceshi.get:=false;
     number:=list.Add(ceshi);
     Self.mmo1.Clear;
     List.Sort; //排序  应该按照第一数排序

      if List.Count<>0 then  //这里要注意容错处理
      begin
        Self.mmo1.Lines.Add(inttostr(list.Items[0].sky));
      end else


      begin
        Self.mmo1.Lines.Add('list is clear;');
      end;
      end;




end;
procedure TForm1.FormCreate(Sender: TObject);
begin
   List := TList<TGamelist>.Create;
   cslist:=tstringlist.Create;
   mslist:=tstringlist.Create;
end;


procedure TForm1.btn2Click(Sender: TObject);
var
 js,jsa:Integer;
begin
   if List.Count<>0 then
   begin
      js:=List.Count;
      for jsa := 0 to js - 1 do
       begin
         form1.lst2.Items.add(inttostr(list.Items[jsa].sky));
       end;

   end;
end;
procedure  iddmwupinxinxi(srstring:tstringlist);
var
  jssI,jssaI,ss:Integer;
begin
   jssI:=srstring.Count;
  // cslist.clear;
   for jssaI := 0  to jssI - 1 do
   begin
     if strtoint(srstring.Strings[jssaI])>10 then
      begin
         cslist.add(srstring.Strings[jssaI]);
      end;
   end;
   ss:=cslist.Count;
end;
procedure TForm1.btn3Click(Sender: TObject);
 var
ii,qq,qqa,yy,yya:Integer;
begin
  qq:=Form1.lst2.Items.count;
  for qqa := 0 to qq - 1 do
  begin
    mslist.add(Form1.lst2.Items[qqa]);
  end;

 iddmwupinxinxi(mslist);
  if cslist.count<>0 then
    begin
      yy:=cslist.count;
      for yya := 0 to yy - 1 do
      begin
          form1.mmo2.Lines.Add(cslist.Strings[yya]);
      end;

    end;
  ii:=mslist.count;
  form1.lbl1.Caption:=IntToStr(ii);
end;



procedure TForm1.FormDestroy(Sender: TObject);
begin
    List.Free;
    cslist.Free;


    mslist.free;
end;

end.



里面我定义了 一个结构体 然后通过一个泛型list添加这个结构体
然后我定义了一个函数 主要就是操作结构体的数据 修改完 之后在放入到list
关键是我在函数外可以操作的到函数里就不可以了。用了很多方式都无法正确的添加 2010 泛型 Delphi lsit?结构体
[解决办法]
var
 List: TList<pGamelist>;
[解决办法]
list.Add(mGamelist); //这里提示出错 ,但是希望不出错原因估计是泛型数组不支持这种操作吧
这样当然出错

应该用:
list.Add(mGamelist^);
[解决办法]
你得到指针做啥?如果要往TList中加入的是指针,那么当然要定义为:
lst: TList<pGamelist>;

并且你在加入的时候,不是定义为一个实例,
ceshi:TGamelist;
 ceshitwo:TGamelist;
这里不应该这样定义,而是定义为
ceshi:pGamelist;
 ceshitwo:pGamelist;

使用时创建:
New(ceshi);
New(ceshitow);
...
list.Add(ceshi);
....

热点排行