关于THashedStringList.AddObject求助
会点delphi,接触c++时间不长,问题如下:sl在Form的Create函数里初始化(sl= new THashedStringList;)
AnsiString aa=Qry1->FieldByName("xm")->AsString;能正常显示选定的记录的值。但是放到循环里就不行了。
char* p=aa.c_str();
sl->AddObject("a",(TObject*)"1");
sl->AddObject("b",(TObject*)"2");
sl->AddObject("c",(TObject*)p);
sl->AddObject("d",(TObject*)(String*)"6");
int i=sl->IndexOf("c");
if (i!=-1)
ShowMessage(sl->Strings[i]+"="+(char*)sl->Objects[i]);
for (int i=0;i<5;i++)下面是用一个按钮显示List里的值:
{
AnsiString dztm= Qry1->FieldByName("dztm")->AsAnsiString;
AnsiString sXM= Qry1->FieldByName("xm")->AsAnsiString ;
char * pXM;
pXM = sXM.c_str() ;
sl->AddObject(dztm,(TObject*)pXM) ;
Qry1->Next();
}
void __fastcall TForm1::BitBtn2Click(TObject *Sender)显示的结果为空,偶尔有乱码,总之不是正确的结果。Qry1是没有问题的,有1w条数据,且都有xm。请达人解惑
{
int nIndex=sl->IndexOf(AnsiString(Edit1->Text)) ;
if (nIndex!=-1)
{
AnsiString xm=(char *) sl->Objects[nIndex];
ShowMessage(xm );
}
else ShowMessage(IntToStr(nIndex)+"Err");
}
AnsiString dztm, sXM;
for (int i = 0; i < 5; i++)
{
dztm = Qry1->FieldByName("dztm")->AsAnsiString;
sXM = Qry1->FieldByName("xm")->AsAnsiString;
char *p = new char[sXM.Length() + 1];
strcpy(p, sXm.c_str());
p[sXM.Length] = 0x0;
sl->AddObject(dztm, (TObject *)p);
Qry1->Next();
}
for (int i = 0; i < 5; i++)
delete [](char *)(sl->Objects[i]);