0

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")
            {

            }
        }
    }
}
2
  • Why not use something like SQLite so you can use the built in database features to do this for you? Commented Oct 19, 2013 at 19:11
  • Please check an answer or add more help to us Commented Mar 15, 2014 at 8:56

3 Answers 3

2

The C# way is to take usage of LINQ to query collections, as such:

var persons = personArrayList.AsQueryable();
var females = persons.Where(p => p.gender.Equals("Female"));
Sign up to request clarification or add additional context in comments.

6 Comments

I know this does not work as this is an ArrayList and not a List<Person> but I wonder can you translate the full query like I use it to this format? "from Person P in personArrayList where P.Gender == "Female" select P;"
in your case my compiler can't find out what type p is. so it can never get to p.gender... could it be because I use .net 3.5 here?
You need to type your arraylist: personArrayList = new ArrayList<Person>();
Yea but from the soure code it is a plain arraylist... that is why I asked how to specify the type of p in your system.... I know it is the same for IL but I don't fully understand your system
Okay. You have the option to cast the collection in your method of course, but I don't see any reason for not typing it at initialization. I'm on a Mac atm so can't try it, but you could try casting p in the lambda: Where((Person)p => p.gender.Equals("Female"))
|
2

I think something like this should do the trick

using System.Linq;
var females = from Person P in personArrayList where P.Gender == "Female" select P;

[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.

Comments

0

Do you thank something like that?

protected void btnShowGentle_Click(object sender, EventArgs e)
{
    ListBoxShow.Items.Clear();
    for (int i = 0; i < personArrayList.Count; i++)
    {
        if(personArrayList[i].gender == "Male")
            ListBoxShow.Items.Add(personArrayList[i].ToString());
    }
}

////Possible alternate
//ListBoxShow.Items.Clear();
//ListBoxShow.Items.AddRange(personArrayList.Where( x => x.gender == "Male"));

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.