473,403 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Arraylist question

//I had a class

public class PERSON
{
PERSON(string LName, string FName, string age)
{
m_last = LName;
m_first = FName;
m_age = age;
}
public string LastName
{
get {return m_last;}
set {m_last = value;}
}
public string FirstName
{
get {return m_first;}
set {m_first = value;}
}
public string PersonAge
{
get {return m_age;}
set {m_age = value;}
}
string m_last;
string m_first;
string age;
}

//In Form1 I do this:

ArrayList CollectionList = new ArrayList();

for(int i = 0; i < Person.Length; i++)
{
CollectionList.Add( new PERSON(LastN, FirstN, PAge));
}

foreach (PERSON per in CollectionList)
{
this.LastcomboBox.Text = per.LastName.ToString();
this.FirstcomboBox.Text = per.FirstName.ToString();
}

//My question is: if I had a list of LastName and FirstName display in a
ComBoBox. How do I display Age in TextBox? when user pick the person
LastName it also show that person age in a TextBox?

Any help are appreciated,



Nov 17 '05 #1
8 1552
First of all, you have a flaw in your user interface design. If you
have one combo box for last name and another for first name, what's to
stop your user from choosing invalid combinations of first and last
name that don't correspond to any Person on your list?

Even if you did prevent that (and it would be tricky), if you have two
people with the same last name then you're going to display that last
name twice in your Lastcombobox list. Ugly.

In order to fix up that situation, I suggest that you add some methods
to your Person class:

Change Person to implement IComparable:

public class Person : IComparable

this will force you to implement the Compare method:

public int CompareTo(object obj)
{
return CompareTo(obj as Person);
}

public int CompareTo(Person otherPerson)
{
int result;
if (otherPerson == null)
{
// Everything is greater than null
result = 1;
}
else
{
result = this.m_last.CompareTo(otherPerson.m_last);
if (result == 0)
{
result = this.m_first.CompareTo(otherPerson.m_first);
}
if (result == 0)
{
result = this.age.CompareTo(otherPerson.age);
}
return result;
}
}

Now, you can sort your array of Persons by last name then first name:

CollectionList.Sort();

Once you've done that, I would put all of the "Person"s in one combo
box, using a last name / first name display. I would create a new
property in Person:

public string ComboBoxDisplay
{
get { return this.m_last + ", " + this.m_first; }
}

then add that string to the combo box for each Person:

foreach (Person per in CollectionList)
{
this.PersoncomboBox.Items.Add(per.ComboBoxDisplay) ;
}

Now you have to hook up an event handler to your combo box, and write a
method to say what will happen whenever the user changes the selected
index in the combo box:

this.PersoncomboBox.SelectedIndexChanged += new
System.EventHandler(PersoncomboBox_SelectedIndexCh anged);

....

private void PersoncomboBox_SelectedIndexChanged(object sender,
System.EventArgs ea)
{
// Unfortunately, ComboBox doesn't have a Tag attribute, so you
have
// to find the chosen Person by position
int selected = this.PersoncomboBox.SelectedIndex;
if (selected < 0)
{
this.txtAge.Text = "";
}
else
{
Person chosenPerson = (Person)this.CollectionList[selected];
this.txtAge.Text = chosenPerson.PersonAge;
}
}

Nov 17 '05 #2
Another way to do this, which would not use a collection is to override the
ToString virtual method in your PERSON class to return:

public override string ToString()
{
return m_last + "," + m_first;
}

Now you can add the instances of person directly to the combobox which will
use the ToString() method to put the display text in the combobox drop down
list. i.e

PERSON p1 = new PERSON("last", "first", 22);
comboBox1.Add(p1);

When you want to get the value back you can simply get the PERSON object
back out of the Combobox by using the SelectedItem property i.e.

private void comboBox1_SelectedIndexChanged(object sender,
System.EventArgs ea)
{
PERSON selectedPerson = comboBox1.SelectedItem as PERSON;

if(selectedPerson != null)
{
textBoxAge.Text = selectedPerson.Age;
}
}

Mark R Dawson

"Bruce Wood" wrote:
First of all, you have a flaw in your user interface design. If you
have one combo box for last name and another for first name, what's to
stop your user from choosing invalid combinations of first and last
name that don't correspond to any Person on your list?

Even if you did prevent that (and it would be tricky), if you have two
people with the same last name then you're going to display that last
name twice in your Lastcombobox list. Ugly.

In order to fix up that situation, I suggest that you add some methods
to your Person class:

Change Person to implement IComparable:

public class Person : IComparable

this will force you to implement the Compare method:

public int CompareTo(object obj)
{
return CompareTo(obj as Person);
}

public int CompareTo(Person otherPerson)
{
int result;
if (otherPerson == null)
{
// Everything is greater than null
result = 1;
}
else
{
result = this.m_last.CompareTo(otherPerson.m_last);
if (result == 0)
{
result = this.m_first.CompareTo(otherPerson.m_first);
}
if (result == 0)
{
result = this.age.CompareTo(otherPerson.age);
}
return result;
}
}

Now, you can sort your array of Persons by last name then first name:

CollectionList.Sort();

Once you've done that, I would put all of the "Person"s in one combo
box, using a last name / first name display. I would create a new
property in Person:

public string ComboBoxDisplay
{
get { return this.m_last + ", " + this.m_first; }
}

then add that string to the combo box for each Person:

foreach (Person per in CollectionList)
{
this.PersoncomboBox.Items.Add(per.ComboBoxDisplay) ;
}

Now you have to hook up an event handler to your combo box, and write a
method to say what will happen whenever the user changes the selected
index in the combo box:

this.PersoncomboBox.SelectedIndexChanged += new
System.EventHandler(PersoncomboBox_SelectedIndexCh anged);

....

private void PersoncomboBox_SelectedIndexChanged(object sender,
System.EventArgs ea)
{
// Unfortunately, ComboBox doesn't have a Tag attribute, so you
have
// to find the chosen Person by position
int selected = this.PersoncomboBox.SelectedIndex;
if (selected < 0)
{
this.txtAge.Text = "";
}
else
{
Person chosenPerson = (Person)this.CollectionList[selected];
this.txtAge.Text = chosenPerson.PersonAge;
}
}

Nov 17 '05 #3
How do I clear all the elements in the the arraylist and the class to get the
new info in, such as age is changing, I want to update with the latest age?
I try to get new info to the arraylist but the problem I got is the
arraylist keep adding up with multiple entry for the same person. I try
clear() but that doesn't help. Please help.

thanks

"Mark R. Dawson" wrote:
Another way to do this, which would not use a collection is to override the
ToString virtual method in your PERSON class to return:

public override string ToString()
{
return m_last + "," + m_first;
}

Now you can add the instances of person directly to the combobox which will
use the ToString() method to put the display text in the combobox drop down
list. i.e

PERSON p1 = new PERSON("last", "first", 22);
comboBox1.Add(p1);

When you want to get the value back you can simply get the PERSON object
back out of the Combobox by using the SelectedItem property i.e.

private void comboBox1_SelectedIndexChanged(object sender,
System.EventArgs ea)
{
PERSON selectedPerson = comboBox1.SelectedItem as PERSON;

if(selectedPerson != null)
{
textBoxAge.Text = selectedPerson.Age;
}
}

Mark R Dawson

"Bruce Wood" wrote:
First of all, you have a flaw in your user interface design. If you
have one combo box for last name and another for first name, what's to
stop your user from choosing invalid combinations of first and last
name that don't correspond to any Person on your list?

Even if you did prevent that (and it would be tricky), if you have two
people with the same last name then you're going to display that last
name twice in your Lastcombobox list. Ugly.

In order to fix up that situation, I suggest that you add some methods
to your Person class:

Change Person to implement IComparable:

public class Person : IComparable

this will force you to implement the Compare method:

public int CompareTo(object obj)
{
return CompareTo(obj as Person);
}

public int CompareTo(Person otherPerson)
{
int result;
if (otherPerson == null)
{
// Everything is greater than null
result = 1;
}
else
{
result = this.m_last.CompareTo(otherPerson.m_last);
if (result == 0)
{
result = this.m_first.CompareTo(otherPerson.m_first);
}
if (result == 0)
{
result = this.age.CompareTo(otherPerson.age);
}
return result;
}
}

Now, you can sort your array of Persons by last name then first name:

CollectionList.Sort();

Once you've done that, I would put all of the "Person"s in one combo
box, using a last name / first name display. I would create a new
property in Person:

public string ComboBoxDisplay
{
get { return this.m_last + ", " + this.m_first; }
}

then add that string to the combo box for each Person:

foreach (Person per in CollectionList)
{
this.PersoncomboBox.Items.Add(per.ComboBoxDisplay) ;
}

Now you have to hook up an event handler to your combo box, and write a
method to say what will happen whenever the user changes the selected
index in the combo box:

this.PersoncomboBox.SelectedIndexChanged += new
System.EventHandler(PersoncomboBox_SelectedIndexCh anged);

....

private void PersoncomboBox_SelectedIndexChanged(object sender,
System.EventArgs ea)
{
// Unfortunately, ComboBox doesn't have a Tag attribute, so you
have
// to find the chosen Person by position
int selected = this.PersoncomboBox.SelectedIndex;
if (selected < 0)
{
this.txtAge.Text = "";
}
else
{
Person chosenPerson = (Person)this.CollectionList[selected];
this.txtAge.Text = chosenPerson.PersonAge;
}
}

Nov 17 '05 #4
You would not clear the array list and add the Person objects again
when they change.

The array list does not contain the Person objects... it contains
references to the Person objects, which have a life of their own. Think
of it like having an address book with your friends' phone numbers in
it. When one of your friends has a birthday party, you don't erase
their information in your address book, do you? You don't do that
because what is in your address book is not your friend himself, but
just a reference to your friend, who has a life of his own and
qualities all his own that don't depend upon what's written in your
book.

When you say

Person chosenPerson = (Person)this.CollectionList[se*lected];

you are *not* getting a *copy* of the Person in chosenPerson... you are
getting the original Person back again. So, you can say:

chosenPerson.Age += 1;

and then next time you say:

Person aPerson = (Person)this.CollectionList[se*lected];

the person's age will be one more than at the start, just as you last
left it.

Nov 17 '05 #5
Hi Bruce,

The reason I want to clear the array list and the Person objects is because
I get the person info from other application, it keep updating new info every
10-15 minutes.
There are more elements in Person class beside Last, First name and age. I
just make it simple for easy to understand.
So if I wanted to update this array list, how do I do it?

thanks

"Bruce Wood" wrote:
You would not clear the array list and add the Person objects again
when they change.

The array list does not contain the Person objects... it contains
references to the Person objects, which have a life of their own. Think
of it like having an address book with your friends' phone numbers in
it. When one of your friends has a birthday party, you don't erase
their information in your address book, do you? You don't do that
because what is in your address book is not your friend himself, but
just a reference to your friend, who has a life of his own and
qualities all his own that don't depend upon what's written in your
book.

When you say

Person chosenPerson = (Person)this.CollectionList[seÂ*lected];

you are *not* getting a *copy* of the Person in chosenPerson... you are
getting the original Person back again. So, you can say:

chosenPerson.Age += 1;

and then next time you say:

Person aPerson = (Person)this.CollectionList[seÂ*lected];

the person's age will be one more than at the start, just as you last
left it.

Nov 17 '05 #6
How do you know that the other application has made a change? Or do you
just clear things out and refresh every 10-15 minutes?

Nov 17 '05 #7
Hi Bruce,

The reason I know is because the developer for other application notify me
they are updating the information every 10-15 minutes. So, what if I want to
clear things out and refresh every 10-15 minutes, how do I do that?

thanks,
Hannah

"Bruce Wood" wrote:
How do you know that the other application has made a change? Or do you
just clear things out and refresh every 10-15 minutes?

Nov 17 '05 #8
Hi Bruce,

The reason I know is because the developer for other application notify me
they are updating the information every 10-15 minutes. So, what if I want to
clear things out and refresh every 10-15 minutes, how do I do that?

thanks,
Hannah

"Bruce Wood" wrote:
How do you know that the other application has made a change? Or do you
just clear things out and refresh every 10-15 minutes?

Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Alex Ting | last post by:
Hi Everybody, I have an issue about deleting an object from an arrayList. I've bounded a datagrid using this code where it will first run through all of the code in loadQuestions() and bind a...
3
by: george r smith | last post by:
I am trying to create an arrayList that contains multiple arrayLists. My code attempt is below. The question I have is how can I get away from creating another pAttribute list than can be added to...
10
by: C Downey | last post by:
Hello: I have an arraylist storing some very basic objects. The object is very basic, it has 2 properties : ID, and COUNT Before I add an object to the arraylist, I want to check if an...
6
by: GrandpaB | last post by:
While writing this plea for help, I think I solved my dilemma, but I don't know why the problem solving statement is necessary. The inspiration for the statement came from an undocumented VB...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
3
by: Mark Jones | last post by:
I am quite new to ASP and .Net development and I am building a web-based multiple choice exam application. The web page displays the questions using a Repeater control and the answers are nested...
11
by: Simon Says | last post by:
Hi all, I've an arraylist A1 that contains {0,1,2,3,4,5,9,8,7} I've another arraylist A2 that contains {4,5,9} I would like to search whether if A2 is present in A1 and maybe return the first...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
3
by: Christopher H | last post by:
I've been reading about how C# passes ArrayLists as reference and Structs as value, but I still can't get my program to work like I want it to. Simple example: ...
1
by: =?Utf-8?B?SkI=?= | last post by:
Hello My pgm1 (User Interface Level) passes an empty ArrayList to pgm2 (Business Logic Level). pgm2 then calls pgm3 (Data Access Level) to populate the ArrayList. Question1: When pgm2 gets...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.