PDA

View Full Version : سوال: FindIndex در جنریک لیست



rostamkhani
سه شنبه 10 دی 1387, 02:30 صبح
سلام
من برای جستجوی مقادیر textBox در یک لیست از FindIndex به شکل زیر استفاده کردم.
روش بهتری برای این کار هست ؟
(روشی که مثلا نیاز به متغیر strFind نباشه)


List<string> listInt = new List<string>();
public Form1()
{
InitializeComponent();
listInt.Add("ali");
listInt.Add("reza");
listInt.Add("mohamad");
listInt.Add("sadegh");
listInt.Add("ahmad");
listInt.Add("fazel");
listInt.Add("hasan");
listInt.Add("hosein");
listInt.Add("fazel");

}
private void textBox1_TextChanged(object sender, EventArgs e)
{
strFind=textBox1.Text;
this.Text = listInt.FindIndex(FindIndex).ToString();
}
public static string strFind = "";
private static bool FindIndex(String str)
{
if (str == strFind)
{
return true;
}
else
{
return false;

sinpin
سه شنبه 10 دی 1387, 07:53 صبح
به جای :

private void textBox1_TextChanged(object sender, EventArgs e)
{
strFind = textBox1.Text;
this.Text = listInt.FindIndex(FindIndex).ToString();
}

public static string strFind = "";
private static bool FindIndex(String str)
{
if (str == strFind)
{
return true;
}
else
{
return false;
}
}میتونید بنویسید:

private void textBox1_TextChanged(object sender, EventArgs e)
{
this.Text = listInt.FindIndex(s => s.Equals(textBox1.Text)).ToString();
}

mehdi.mousavi
سه شنبه 10 دی 1387, 15:07 عصر
سلام من برای جستجوی مقادیر textBox در یک لیست از FindIndex به شکل زیر استفاده کردم. روش بهتری برای این کار هست ؟ (روشی که مثلا نیاز به متغیر strFind نباشه)

سلام.
دلیلی نداره برای همچین Logic ای از FindIndex استفاده کنید!
راه صحیحش، استفاده از IndexOf هست:


Int32 pos = listInt.IndexOf(textBox1.Text);