473,508 Members | 2,434 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to check if an object already exists in collection?

3 New Member
Hi All,

I am learning programming and my problem is that I have a bunch of objects and I want to add these objects to a list only if the list does not already contain that object. Secondly if the object is already contained I want to ignore that object and add the next one instead. I think I have got the first part working just need some help with part two. Many thanks.

Ben

Expand|Select|Wrap|Line Numbers
  1. PartyGroup partyGroup = new PartyGroup(); 
  2.  
  3. using (AseDataReader reader = command.ExecuteReader())
  4.             {
  5.                 while (reader.Read())
  6.                 {
  7.                     if (!myPartyGroupList.Contains(partyGroup))
  8.                     {
  9.                         partyGroup.PartyGroupID = Convert.ToInt32(reader["party_group_id"]);
  10.                         partyGroup.PartyGroupName = reader["party_group_name"].ToString();
  11.                         partyGroup.PersonList = myPersonList;
  12.  
  13.                         myPartyGroupList.Add(partyGroup);
  14.  
  15.                     }
  16.                     else
  17.                     {
  18.                         //??
  19.  
  20.                     }
  21.  
  22.  
  23.                 }
  24.             }
Dec 17 '10 #1
2 4577
Plater
7,872 Recognized Expert Expert
It looks like you're already doing the second part.
If its already in the collection you just ignore it and read the next value from the reader.
Although, I don't know how you go from reader.read() to partygroup
Dec 17 '10 #2
hype261
207 New Member
Have you considered using C# SortedSet<T> or HashSet<T> generic collections? They provide the functionality that I believe you are looking for, No duplicate Items.

Here is a link to the MSDN website.

http://msdn.microsoft.com/en-us/library/dd412070.aspx
Dec 18 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
3624
by: David | last post by:
My email application was working a couple of weeks ago, then all of a sudden I get Error Type: (0x8009000F) Object already exists. I checked the permissions on the machinekeys directory...
1
4705
by: Uttam | last post by:
Hello, How does one suppress the "Object already exists" message when the following statement is executed in ADO? cat.Views.Append "Some_Query", cmd Thanks in advance. Uttam
5
23022
by: BerkshireGuy | last post by:
Hello everyone, I have a bond form that a user uses to enter data. One of my fields, is PolicyNumber. I added some code on the Before Update event of txtPolicyNumber that checks to see if...
9
7239
by: Carl Fenley | last post by:
I am successfully adding stored procedures to an Access database. However, I need to be able to check if the stored procedure of the same name already exists. Is there a way to do this other...
4
1339
by: Lee Gillie | last post by:
What I am hoping to do is make a constructor for a derived class that can accept an instance of the base class. Maybe this just is not possible? I am working with the new SSLStream in the 2005...
8
11629
by: bob | last post by:
I am writing some code to create new tables in a SQL database. However, I don't want to try to create a table if it already exists. How can I test beforehand to see if a particular named table...
0
489
by: Alun Jones | last post by:
I'm getting the above error in a dialog box from Visual Studio 2005 when trying to sign an assembly using a PFX file, and would like to know how to resolve the problem. Background: The PFX...
2
5986
by: =?Utf-8?B?R2FicmllbCBNw6luZGV6?= | last post by:
Hello everyone. I have a small class that encapsulates some functionallity to work with the RSACryptoServiceProvider. Here is the code of the class i'm using: public class dsRSA { private...
59
32698
oll3i
by: oll3i | last post by:
how to check if an object already exists and return reference to that object
4
13039
gundarap
by: gundarap | last post by:
Hello all, I'm working on minidom. My goal is to see whether an element already exists in the xml file before adding. I was using getElementsByTagName() to check weather the element already exists....
0
7224
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,...
1
7038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5625
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,...
1
5049
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...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.