如何传出FOR循环的值?

l285791700 2008-09-04 09:38:10
public string liB()
{
string a;
for (int i = 0; i < listBox1.Items.Count; i++)
{
a = i.ToString();


}
return a;
}

请问如何能返回A的值???
...全文
239 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
fanerde 2008-09-04
  • 打赏
  • 举报
回复
~
return listBox1.Items[listBox1.Items.Count - 1].ToString();
l285791700 2008-09-04
  • 打赏
  • 举报
回复
谢谢大家谢谢大家~~~~~~~~~~~~~~~~~~~~~
我以后会努力的
wdzr_826 2008-09-04
  • 打赏
  • 举报
回复
return listBox1.Items[listBox1.Items.Count - 1].ToString();



public string liB()
{
string a="";
for (int i = 0; i < listBox1.Items.Count; i++)
{
a = listBox1.Items[i].ToString();// i.ToString();
}
return a;
}
LovingAlison 2008-09-04
  • 打赏
  • 举报
回复
要返回最后一项 应该不用循环就取得到

listBox1.Items[listBox1.Items.Count-1].Text 试试看
Annie_Cai 2008-09-04
  • 打赏
  • 举报
回复
public string liB()
{
string a="";
for (int i = 0; i < listBox1.Items.Count; i++)
{
a = listBox1.Items[i].ToString();// i.ToString();
}
return a;
}
wuyi8808 2008-09-04
  • 打赏
  • 举报
回复
return listBox1.Items[listBox1.Items.Count - 1].ToString();
Annie_Cai 2008-09-04
  • 打赏
  • 举报
回复

public string liB()
{
string a="";
for (int i = 0; i < listBox1.Items.Count; i++)
{
a = i.ToString(); ;
}
return a;
}
ppp7p 2008-09-04
  • 打赏
  • 举报
回复
你只需要最后一项不需要循环。 return (listBox1.Items.Count - 1).ToString();
shawn_yang 2008-09-04
  • 打赏
  • 举报
回复
直接取listBox1.Items.Count不就ok了,还循环什么
  • 打赏
  • 举报
回复
return (listBox1.Items.Count - 1).ToString()
wanhui219 2008-09-04
  • 打赏
  • 举报
回复
看得出来,LZ没有C语言的基础,导致一个简单的程序逻辑都没有想清
zhengshaodong 2008-09-04
  • 打赏
  • 举报
回复
public string liB()
{
if(listBox1.Items.Count == 0)
{
return "";
}
return listBox1.Items[listBox1.Items.Count-1].Text; //要返回Text还是Value自己选 如果是返回listBox1.Items[最后一项].ToString()的话返回的是Object。。。。什么的吧?

}
l285791700 2008-09-04
  • 打赏
  • 举报
回复
就是现在提示return返回的是空值,我想返回listBox1的最后一项
Bullatus 2008-09-04
  • 打赏
  • 举报
回复
[Quote=引用楼主 l285791700 的帖子:]
public string liB()
{
string a;
for (int i = 0; i < listBox1.Items.Count; i++)
{
a = i.ToString();


}
return a;
}

请问如何能返回A的值???
[/Quote]
return a不就是返回了么

  • 打赏
  • 举报
回复
啥意思啊?
hustcyb 2008-09-04
  • 打赏
  • 举报
回复

public IEnumerable<string> Lib()
{
for (int i = 0; i < listBox1.Items.Count; i++)
{
yield return i.ToString();
}
}

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧