简体   繁体   中英

Arraylist search string but no output

Can anyone tell me, why I don't get any output with this on buttonclick ?

string searchString = TextBox1.Text;
ArrayList personarraylist = new ArrayList();
foreach (Person a in personarraylist)
{
    if (searchString == Convert.ToString(a))
    {
         personarraylist.Add(a);
    }
}
ListBox1.DataSource = personarraylist;

EDIT:

Hi everybody, thanks for your input. I have a class for Person, and all data created on the pages is stored in a file: FileController.ReadFile(Server.MapPath("~/App_Data/Personfile.ser")); This is where I need my data from. I use a Filecontroller class to write and read (also update) to and from the file. But the Search function has just gotten the better part of me. (as a student the data handling in file was required, or I would have used a DB). Hope this clears my code up a little. And do I have to compare the search term to something eg firstName, or can it work through the whole file?

You create a new array list and then immediately foreach over it. Since you just created it there are no items in it, so the foreach does nothing.

EDIT: Um, you're looking in an empty ArrayList :

ArrayList personarraylist = new ArrayList();
foreach (Person a in personarraylist)
{
    ...
}

That's never going to enter the body of the ArrayList . I suspect you meant something like:

ArrayList people = GetAllPeopleFromSomewhere();
foreach (Person a in people)
{
    ...
}

Additionally, even if you did have some values to look through, it's entirely possible that Convert.ToString(a) wouldn't return the searched-for value. Unless your Person class overrides ToString() , you'll just get the class name. It's more likely that you actually want something like:

if (a.FirstName == searchString)

(Or whatever property of Person you actually want to search for.)

Then, you're currently adding to the same ArrayList that you're searching through - that's not what you want. You might want something like:

ArrayList people = GetAllPeopleFromSomewhere();
ArrayList matches = new ArrayList();
foreach (Person a in people)
{
    if (a.FirstName == searchString)
    {
        matches.Add(a);
    }
}

I'd also suggest using generic collections, and ideally LINQ. For example, with LINQ your entire code could be converted to something like this:

ListBox1.DataSource = GetAllPeople().Where(p => p.FirstName == searchString);

You are converting the Person object to a string. You might want to be looking at an element within the Person object for the data you want to compare against.

string searchString = TextBox1.Text;
ArrayList personarraylist = new ArrayList();
foreach (Person a in originalpersonarraylist)
{
    //Assuming that there is a Name property in Person Class.
    if (a.Name.StartsWith(searchString))
        personarraylist.Add(a);
}
ListBox1.DataSource = personarraylist;

You need to fill the personarraylist with values before commencing foreach loop. Something like this should be more usful:

    string searchString = Textbox1.Text;
//Retrive the person data for some location
ArrayList personarraylist = getPersonData();
foreach (Person a in personarraylist)
{
    if (searchString.contains(a.ToString()))
    {
        personarraylist.Add(a);
    }
}
ListBox1.DataSource = personarraylist;

Required operation can be completed in rather straightforward and efficient way using regular array ( string[] ) instead of ArrayList . The solution is outlined below:

  1. Read the entire file as a single string and assign it to string _tmp var (use any object/methods that you are familiar with, for eg System.IO.StreamReader )

  2. Apply Split() method to get string[] array from _tmp :

    string[] _arrPerson= _tmp.Split(string[] separator) ;

  3. Populate ListBox1 from that array _ arrPerson based on a search condition that you have specified using Items.Add() method of ListBox1 :

    for (int i=0; i<_arrPerson.Length;i++) { if (_arrPerson[i].Contains(searchString)) {ListBox1.Items.Add(_arrPerson[i]);} }

This code snippet should run much faster than all that variations utilizing ArrayList .

Hope this will help. My Best, AB

PS: see MSDN link for details on String.Split() method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM