I have a class with some attributes. For example class person with name, age, gender and so on as attribute.
People fill out a form, with among other things their gender which is a dropdownlist, they submit it and the person is added to the arraylist.
What I need is a function by clicking a button to display all the persons who chose Female as gender.
Can somebody please help me? I have tried and searched for the right answer for days now and getting a little desperat now.
Many thanks!!!
Olaf
This is my .cs code
public class Person
{
private string name;
private string artistname;
private string address;
private double number;
private double zip;
private string day;
private string gender;
public Person(string name, string artistname, string address, double number, double zip, string day, string gender)
{
this.name = name;
this.artistname = artistname;
this.address = address;
this.number = number;
this.zip = zip;
this.day = day;
this.gender = gender;
}
public override string ToString()
{
string newPerson = name + " aka " + artistname + " lives on " + address + " " + number + " " + zip + " " + day + "Gender: " + gender;
return newPerson;
}
}
And this is my .aspx code:
public partial class Index : System.Web.UI.Page
{
static ArrayList personArrayList;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
personArrayList = new ArrayList();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
Person p = new Person(txtName.Text, txtArtistName.Text, txtAddress.Text, Convert.ToDouble(txtNumber.Text), Convert.ToDouble(txtPostal.Text), Convert.ToString(dropdownDay.Text), Convert.ToString(dropdownGender.Text));
personArrayList.Add(p);
}
protected void btnShowAll_Click(object sender, EventArgs e)
{
ListBoxShow.Items.Clear();
for (int i = 0; i < personArrayList.Count; i++)
{
ListBoxShow.Items.Add(personArrayList[i].ToString());
}
}
protected void btnShowGentle_Click(object sender, EventArgs e)
{
ListBoxShow.Items.Clear();
}
protected void btnShowLadies_Click(object sender, EventArgs e)
{
ListBoxShow.Items.Clear();
for (int i = 0; i < personArrayList.Count; i++)
{
if (personArrayList[i].gender = "Female")
{
}
}
}
}
Do you thank something like that?
I think something like this should do the trick
[Edit] I had some questions about this myself so asked a question Plain ArrayList Linq c# 2 syntaxes (need a conversion) that can be useful to you.
The C# way is to take usage of LINQ to query collections, as such: