0

I am having problems searching through my Arraylist. The array list stores various information about a number of teams such as the image path to their logo and the team name etc. It is being filled from a separate datafile using a StreamReader

I would like the user to input something in a Textbox from a windows form such as the team name and then consequently the program will then search my arraylist for said string and open another form where the information of the searched team will be loaded up on screen using the Form.Load procedure

To put it simply.

private void btn_Search_Click(object sender, EventArgs e)
{
   //what code do I write here?
}

I understand that I might be a little to deep here for my current knowledge of coding so help would be appreciated.

EDIT: unfortunately it must be in an arraylist, sorry for the inconvenience.

6
  • 2
    why are you using an ArrayList not List<T>? Commented Apr 12, 2013 at 19:49
  • Can you show use how you populate the arraylist? Commented Apr 12, 2013 at 19:49
  • Why Arraylist Which version of c# are you using? Commented Apr 12, 2013 at 19:49
  • How do you want your search algorithm to work (just search names, exact name search etc.)? Commented Apr 12, 2013 at 19:52
  • I am using an arraylist as the project specifys that all data must be stored in an arraylist. c#4.0. An exact name search would be preferred. Commented Apr 12, 2013 at 19:52

3 Answers 3

4

If you can use LINQ:

string nameToMatch = "Tigers"; //can you tell who's from Michigan?
List<Team> teams = new ArrayList<Team>();
//fill team data here

Team selected = teams.FirstOrDefault(t => t.TeamName.Equals(nameToMatch, StringComparison.OrdinalIgnoreCase));

Something like this should work. (This will match the text exactly but allow the search to be case insensitive. You can read about other options here.)

If you want to match a list of all "partial matches", you can do this instead:

List<Team> matchedTeams = teams.Select(t => t.TeamName.Contains(nameToMatch));

Read here for an extension overload of Contains that takes a StringComparison enum value.

Sign up to request clarification or add additional context in comments.

9 Comments

I would go with FirstOrDefault and then check for null -- as the user could enter anything into that TextBox.
@zimdanen the purpose of this question is how to search through an ArrayList, not semantics on the proper technique for string matching in C#.
@Pheonixblade9: The purpose of SO is to teach fresh young minds how to program correctly.
@zac yes, it is possible to do this, but you would have to loop through the list. And that's super lame. Use the special sauce C# gives you to save time! Unless you're just doing it to learn it.
|
1

If you're unfamiliar with LINQ like I am you could use a foreach loop. Something like this:

String nameToMatch = textBox1.text; //read from the text box
foreach (Object obj in Teams) 
{
   MyTeam team = (MyTeam)obj; //MyTeam is an object you could write that would store team information.
   if (team.TeamName.ToUpper() == nameToMatch.ToUpper()) //case insensitive search.
   {
       FormTeam frmTeam = new FormTeam(team); //windows form that displays team info.
       frmTeam.Visible = true;
       break; //if team names are unique then stop searching.
   }
}

Worst case senario is pretty bad, but for me, at least, it's easier to get my head around than LINQ. Good Luck, hope that helps.

4 Comments

upon scanning again I am not to sure where you are reading in from the textbox here. I understand the if statement is where you are launching a new form. But what exactly is "MyTeam"?
Sorry I should have been more specific about that. "MyTeam" is an object or class that you could write that would hold team information. If you have a class like that. The read from the textbox would occur before the foreach loop.
ahh that makes more sense. I have already created a class it was just slightly confusing as I didn't realise that the textbox would be read in automatically. Is there anything I have to write for this to work. as some of the commands appear to be not working. i.e when I wanted to make an arraylist I needed to input using System.Collections do I need to write something like that for this piece of code?
Oh yeah, "FormTeam" would be another windows form that you would define that would display the team information. You just add another windows form to your project and name it "FormTeam" and add the components to it that you need. Also overload the constructor to take a parameter that populates the data fields you defined in "FormTeam"
0

You can use some codes like this to fill your arraylist:

    // ArrayList class object
    ArrayList arrlist = new ArrayList();

    // add items to arrlist collection using Add method
    arrlist.Add("item 1");
    arrlist.Add("item 2");
    arrlist.Add("item 3");
    arrlist.Add("item 4");
    arrlist.Add("item 5");

and use some codes like this to search in your arraylist

string teamName= this.txtTeamName.Text;
// for loop to get items stored at each index of arrlist collection
for (int i = 0; i < arrlist.Count; i++)
{
    if(arrlist[i].toString()==teamName)
      // open a new form for show the found team details
}

it is a good practice to change the cunstractor of your "Team Details" form to get a "team name"

frmTeamDetails(team myteam)

then use this code in the above FOR statement:

frmTeamDetals frm=new frmTeamDetals(teamName);
frm.ShowDialog();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.