TStringList的IndexOf问题?
void __fastcall TForm1::exitClick(TObject *Sender)
{
AnsiString sSql;
TADOQuery* pADOQuery = new TADOQuery(NULL);
pADOQuery->Connection=this->ADOConnection1;
sSql += "select 权限代码 from am_purview where 用户代号 ='";
sSql += daima->Text + "'";
pADOQuery->SQL->Add(sSql);
pADOQuery->Open();
TStringList* plst=new TStringList();
while(!pADOQuery->Eof)
{
plst->Add(pADOQuery->FieldByName("权限代码")->AsString.Trim());
Memo1->Lines->Add(pADOQuery->FieldByName("权限代码")->AsString.Trim());
//Memo1显示值已经取出来了。。如exit等。。。 数据类型为char
pADOQuery->Next();
}
if(plst->IndexOf(exit->Name)<=0)
{
MessageBox(NULL,"失败","系统提示",MB_OK);
}
else
{
MessageBox(NULL,"恭喜","系统提示",MB_OK);
}
delete pADOQuery;
delete plst;
}
可是提示失败也就是条件为真。下面代码通过,条件为否。所以我判断是红色部分出了问题。肯请大家指点指点。。
void __fastcall TForm1::exitClick(TObject *Sender)
{
TStringList* plst=new TStringList();
plst->Add("open");
plst->Add("exit");
if(plst->IndexOf(exit->Name)<=0)
{
MessageBox(NULL,"失败","提示",MB_OK);
}
else
{
MessageBox(NULL,"恭喜","提示",MB_OK);
}
delete plst;
}
[解决办法]
IndexOf的返回值为0表示有效,所以那个判断条件 应该是 < 0即可,而不是 <= 0
[解决办法]
void __fastcall TForm1::exitClick(TObject *Sender){TStringList* plst=new TStringList(); plst->Add("open"); plst->Add("exit"); if(!plst->IndexOf(exit->Name)) { MessageBox(NULL,"失败","提示",MB_OK); }else{MessageBox(NULL,"恭喜","提示",MB_OK);} delete plst;