473,383 Members | 1,739 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,383 software developers and data experts.

Simple question Related to Generic

DNB


I am using generic in the following example.

But I can figure out how to remove an item in the collection??

Thanks

DNB

------------------------------------------------------------------------------------------------

using System.Collections.Generic;

using System.Text;

protected void Page_Load(object sender, EventArgs e)

{

//Generic List Creation

//List is a Generic Class provided by .Net Framework 2.0

//System.Collections.Generics is the Namespace.
List<PersonmyPerson = new List<Person>();

myPerson.Add(new Person("1 Test"));

//IF I have to remove "2 Test" how can I do that in the code mentioned
below

myPerson.Add(new Person("2 Test"));

myPerson.Add(new Person("3 Test", 24, "tt"));

myPerson.Add(new Person("4 Test", 24, "tt"));


foreach (Person p in myPerson)

{
Response.Write(p.Name);

Response.Write("&nbsp;");

Response.Write(p.Age);

Response.Write("&nbsp;");

Response.Write(p.Address);

Response.Write("&nbsp;");
Response.Write(p.Company);

Response.Write("<br>");

}
//IF I have to remove "2 Test" how can I do that

}

}

class Person

{

int _Age;
public int Age

{

get { return _Age; }

set { _Age = value; }

}

String _Name;
public String Name

{

get { return _Name; }

set { _Name = value; }

}

String _Address;
public String Address

{

get { return _Address; }

set { _Address = value; }

}

String _Company;
public String Company

{

get { return _Company; }

set { _Company = value; }

}
public Person() { }

public Person(String Name)

{

this.Name = Name;

this.Age = 0;

this.Address = String.Empty;

this.Company = String.Empty;

}

public Person(String Name, int Age, String Address)

{

this.Name = Name;

this.Age = Age;

this.Address = Address;

}


}

Nov 27 '07 #1
1 1234
On 2007-11-27 14:24:59 -0800, "DNB" <ii@ii.comsaid:
>

I am using generic in the following example.

But I can figure out how to remove an item in the collection??
You'll need to use one of the methods with the word "Remove" in the name.

Your person class doesn't implement IEquatable or override
Object.Equals(), so the only way as the class is now to use Remove() is
if you retain the reference to the object you added, for later use in
removal.

Alternatives include RemoveAll() and RemoveAt(). With the former, you
could use an anonymous method as your predicate:

myPerson.RemoveAll(delegate (Person person)
{ return person.Name == "2 Test"; });

As the name of the method implies, that would remove all instances
matching your criteria. If you only want to remove the first instance
or you know for sure there is only ever one instance, you could search
the list manually:

for (int iperson = 0; iperson < myPerson.Count; iperson++)
{
if (myPerson[iperson].Name == "2 Test")
{
myPerson.RemoveAt(iperson);
break;
}
}

On average that would cut the operation time in half. RemoveAll() will
always enumerate the whole list, so in situations where you can stop at
the first element you find, on average you'd only have to enumerate
half the list instead of all of it.

Of course, you could always implement IEquatable or override
Object.Equals() in your Person class, if appropriate. Then the
Remove() method could be used directly, by creating a new instance of
Person that would test as equal to the one you want to remove and
passing that instance to Remove().

Pete

Nov 27 '07 #2

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

Similar topics

3
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
16
by: Harlan Messinger | last post by:
1. CSS has five generic font family specifiers: serif, sans-serif, cursive, fantasy, monospace. IE lets the users select a default proportional font and a default monospace font. Is there any way...
1
by: wweems | last post by:
I've got a simple javascript object that represents a small portion of a page. I'd like to "serialize" these objects into something I can then parse up with asp.net and c# on the other end. ...
1
by: Keith R | last post by:
Currently, generic types are not CLS compliant. This puts library authors in a quandry who are faced with three bad alternatives: 1. CLS Compliance but no generics, 2. Using generic types but...
3
by: suzy | last post by:
Hello, I am trying to write a generic tool that accesses a SQL server and reads/updates/deletes/creates records. I want to reference this tool from my asp.net pages to talk to my db. by the...
32
by: someone else | last post by:
hi all I'm a newbie to this group. my apologies if I break any rules. I've wrote a simple program to find the first 1,000,000 primes, and to find all primes within any range (up to 200 *...
2
by: Ray Mitchell | last post by:
Hello, I realize that the source code for vsscanf is available from several sources, such as GNU. However, all such source code I've found so far is filled with cryptic (to me) #ifdefs, system...
7
by: Ray Mitchell | last post by:
Hello, I realize that the source code for vsscanf is available from several sources, such as GNU. However, all such source code I've found so far is filled with cryptic (to me) #ifdefs, system...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.