强制类型装换的问题(100分)

  • 主题发起人 主题发起人 新兵
  • 开始时间 开始时间

新兵

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm1.Button1Click(Sender: TObject);
var
a : int64;
begin
a := 330799000000;
self.ComboBox1.Items.AddObject('a',TObject(a));
a := 330799000001;
self.ComboBox1.Items.AddObject('b',TObject(a));
a := 330799000002;
self.ComboBox1.Items.AddObject('c',TObject(a));
end;

procedure TForm1.Button2Click(Sender: TObject);
var
b : int64;
begin
b := int64(self.ComboBox1.Items.Objects[self.ComboBox1.ItemIndex]);
self.Memo1.Text := inttostr(int64(self.ComboBox1.Items.Objects[self.ComboBox1.ItemIndex]));
end;
上面程序取到的combobox1的值不对,应该怎样处理
 
unit Unit1;

interface

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

type
PAbc = ^Tabc;
TAbc = record
a: Int64;
end;

TForm1 = class(TForm)
ComboBox1: TComboBox;
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
p : PAbc;
begin
New(p);
p.a := 330799000000;
ComboBox1.Items.AddObject('a',TObject(p));
New(p);
p.a := 330799000001;
ComboBox1.Items.AddObject('b',TObject(p));
New(p);
p.a := 330799000002;
ComboBox1.Items.AddObject('c',TObject(p));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Memo1.Text := IntToStr(PAbc(ComboBox1.Items.Objects[ComboBox1.ItemIndex]).a);
end;

end.
 
用完了,别忘了把所有add的object 全部dispose掉. 因为他们都是指针, 如果时对象, create产生的, 则 free掉.
 
procedure TForm1.Button1Click(Sender: TObject);
var
a : Pint64;
begin
New(a);
a^ := 330799000000;
self.ComboBox1.Items.AddObject('a',TObject(a));

New(a);
a^ := 330799000001;
self.ComboBox1.Items.AddObject('b',TObject(a));

New(a);
a^ := 330799000002;
self.ComboBox1.Items.AddObject('c',TObject(a));
end;


procedure TForm1.Button2Click(Sender: TObject);
var
b : int64;
begin
b := Pint64(self.ComboBox1.Items.Objects[self.ComboBox1.ItemIndex])^;
self.Memo1.Text := inttostr(b);
end;

别忘了dispose
 
请问,如何free掉,怎样写
 
procedure TForm1.FormDestroy(Sender: TObject);
var
i: integer;
a : Pint64;
begin
for i:=0 to ComboBox1.Items.Count - 1 do
begin
a:=Pint64(ComboBox1.Items.Objects);
if a<>nil then dispose(a);
end;
end;
 
上面的程序运行没问题,可换到我程序中怎么还是有错呀
 
下面是我的程序,请帮忙看一下,先谢了(注,我用的是dev的cxComboBox,应该一样)
////form初始化,设置combobox的值
procedure TOperationDemoBaseFrame.FrameInitialization(bjlx : integer);
var
i : integer;
str : string;
procedure GetListValueOfString(sql : string
temp : TcxComboBox);
var
oratempqry : TOraQuery;
Pjgbm : Pint64
/////////这里是后改的
begin
oratempqry := TOraQuery.Create(nil);
try
new(Pjgbm);/////这里也是
oratempqry.Session := DMPolice.OraSession;
oratempqry.Close;
oratempqry.SQL.Text := sql;
oratempqry.Open;
if Not oratempqry.IsEmpty then
begin
while Not oratempqry.Eof do
begin
Pjgbm^ := oratempQry.FieldByName('a').asinteger;///这里也是
temp.Properties.Items.AddObject(oratempqry.fieldbyname('b').AsString,Tobject(Pjgbm));///这里也是
oratempqry.Next;
end;
end;
finally
oratempqry.Free;
end;
end;
begin
DMPolice.IsFrameFieldChange := False;
for i := 0 to self.ControlCount - 1 do
begin
if self.Controls is TcxComboBox then
begin
case TcxComboBox(self.Controls).Tag of
4 : begin
case bjlx of
0 : str := 'select jgbm a,jgmc b from zzjgdmb where jglx = 0 and dwlbdm in(1) and xs=1 order by px';
1 : str := 'select jgbm a,jgmc b from zzjgdmb where jglx = 1 and dwlbdm in(1,5) and xs=1 order by px';
2 : str := 'select jgbm a,jgmc b from zzjgdmb where jglx = 2 and dwlbdm in(1,3) and xs=1 order by px';
end;
TcxComboBox(self.Controls).Properties.Items.Clear;
GetListValueofString(str,TcxComboBox(self.Controls));////这里调用
TcxComboBox(self.Controls).Properties.OnChange := self.FrameFieldOnChange;
end;
end;
end;
end;
end;
/////保存方法,从combobox中取出值来
Function TOperationDemoBaseFrame.FrameSaveAll(AControl: TWinControl) : string;
var
i : integer;
temp : int64;
str : string;
begin
for i := 0 to AControl.ControlCount - 1 do
begin
case TwinControl(AControl.Controls).Tag of
1 : begin
if TcxComboBox(AControl.Controls).Text = '' then
str := str + 'bjlbdm = null,'
else
str := str + 'bjlbdm = ' + inttostr(integer(TcxComboBox(AControl.Controls).Properties.items.Objects[TcxComboBox(AControl.Controls).ItemIndex])) + ',';
end;
2 : begin
if TcxComboBox(AControl.Controls).Text = '' then
str := str + 'bjlxdm = null,'
else
str := str + 'bjlxdm = ' + inttostr(integer(TcxComboBox(AControl.Controls).Properties.items.Objects[TcxComboBox(AControl.Controls).ItemIndex])) + ',';
end;
3 : begin
if TcxComboBox(AControl.Controls).Text = '' then
str := str + 'bjxldm = null,'
else
str := str + 'bjxldm = ' + inttostr(integer(TcxComboBox(AControl.Controls).Properties.items.Objects[TcxComboBox(AControl.Controls).ItemIndex])) + ',';
end;
4 : begin//////////就是这里
if TcxComboBox(AControl.Controls).Text = '' then
str := str + 'gxdwdm = null,'
else
temp := Pint64(TcxComboBox(AControl.Controls).Properties.Items.Objects[TcxComboBox(AControl.Controls).ItemIndex])^;
str := str + 'gxdwdm = ' + inttostr(temp) + ',';
end;
5 : begin
if TcxComboBox(AControl.Controls).Text = '' then
str := str + 'jqjb = null,'
else
str := str + 'jqjb = ' + inttostr(integer(TcxComboBox(AControl.Controls).Properties.items.Objects[TcxComboBox(AControl.Controls).ItemIndex])) + ',';
end;
20 : begin
if trim(TcxTextEdit(AControl.Controls).Text) = '' then
str := str + 'afdd = null,'
else
str := str + 'afdd = ''' + trim(TcxTextEdit(AControl.Controls).text) + ''',';
end;
30 : begin
if trim(TcxTextEdit(AControl.Controls).Text) = '' then
str := str + 'bt = null,'
else
str := str + 'bt = ''' + trim(TcxTextEdit(AControl.Controls).text) + ''',';
end;
40 : begin
if trim(TcxTextEdit(AControl.Controls).Text) = '' then
str := str + 'bjnr = null,'
else
str := str + 'bjnr = ''' + trim(TcxTextEdit(AControl.Controls).text) + ''',';
end;
end;
end;
result := str;
end;
 
哎,高手,别走啊
 
后退
顶部