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.
Arraylist search string but no output
-
14-04-2022 - |
Question
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 e g firstName, or can it work through the whole file?
Solution
OTHER TIPS
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:
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 e.g.System.IO.StreamReader
)Apply
Split()
method to getstring[]
array from_tmp
:string[] _arrPerson= _tmp.Split(string[] separator)
;Populate
ListBox1
from that array _arrPerson
based on a search condition that you have specified usingItems.Add()
method ofListBox1
: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