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

ArrayList of my objects

Hi,

I'm trying to use Contains method, but I have some problems ...
// create SqlDataReader
SqlDataReader dr = ....

// create collection of myObjects
MyCollection myObjectsColl = new MyCollection();

while (dr.Read()) {

// Creating myObject
// Sometimes I create myObject using identical values of dr["a"] and
dr["b"]
// aren't those object equivalent ?
MyObject myObject = Getsomething(dr["a"].ToString(),
dr["b"].ToString());

// trying to add myObject, if it's not present already in myCollection
if (!myObjectColl.Contains(myObject)) {
myObjectsColl.Add(myObject);
}

}
I don't know why, but (!myObjectColl.Contains(myObject)) is NEVER false ...

please help, Neven
Nov 19 '05 #1
4 1180
Have you overridden Equals method in your MyObject class to tell how it can
detect to instances of MyObject from each other?

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
"Neven Klofutar" <neven.klofutar*r.e..mo*****@vip.hr> wrote in message
news:Ol*************@TK2MSFTNGP12.phx.gbl...
Hi,

I'm trying to use Contains method, but I have some problems ...
// create SqlDataReader
SqlDataReader dr = ....

// create collection of myObjects
MyCollection myObjectsColl = new MyCollection();

while (dr.Read()) {

// Creating myObject
// Sometimes I create myObject using identical values of dr["a"] and
dr["b"]
// aren't those object equivalent ?
MyObject myObject = Getsomething(dr["a"].ToString(),
dr["b"].ToString());

// trying to add myObject, if it's not present already in myCollection
if (!myObjectColl.Contains(myObject)) {
myObjectsColl.Add(myObject);
}

}
I don't know why, but (!myObjectColl.Contains(myObject)) is NEVER false
...

please help, Neven

Nov 19 '05 #2
Nope ...

Can you tell me what must I do ... or send a useful link ?

thanx, Neven
"Teemu Keiski" <jo****@aspalliance.com> wrote in message
news:Oj**************@TK2MSFTNGP10.phx.gbl...
Have you overridden Equals method in your MyObject class to tell how it can detect to instances of MyObject from each other?

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
"Neven Klofutar" <neven.klofutar*r.e..mo*****@vip.hr> wrote in message
news:Ol*************@TK2MSFTNGP12.phx.gbl...
Hi,

I'm trying to use Contains method, but I have some problems ...
// create SqlDataReader
SqlDataReader dr = ....

// create collection of myObjects
MyCollection myObjectsColl = new MyCollection();

while (dr.Read()) {

// Creating myObject
// Sometimes I create myObject using identical values of dr["a"] and
dr["b"]
// aren't those object equivalent ?
MyObject myObject = Getsomething(dr["a"].ToString(),
dr["b"].ToString());

// trying to add myObject, if it's not present already in myCollection if (!myObjectColl.Contains(myObject)) {
myObjectsColl.Add(myObject);
}

}
I don't know why, but (!myObjectColl.Contains(myObject)) is NEVER false
...

please help, Neven


Nov 19 '05 #3
Here is an example (note that it should also overrde GetHashCode, but can
run without doing that, but couldn't be used reliably with Hashtable unless
implementing that method)

Here is MyObject class which by overriding Equals makes difference between
its instances by comparing id field

===============

public class MyObject
{
//id member to represent identified for the object
private int _id;

public MyObject(int id)
{
this._id = id;
}

public override bool Equals(object obj)
{
//Test if passed object is of same type
MyObject o = obj as MyObject;
if(o != null)
{
//it is, detect if they are same object based on id member data;
return o._id == this._id;
}
return false;
}
}
==========

And here is a usage example with ArrayList comparing with Contains as in
your code

==========

//Create MyObject with ID 1
MyObject obj1=new MyObject(1);

//Create MyObject with ID 2
MyObject obj2=new MyObject(2);

//Create duplicate MyObject with ID 1
MyObject duplicateObj1=new MyObject(1);

ArrayList arrList=new ArrayList();

if(!arrList.Contains(obj1)) {
//THis will be called
arrList.Add(obj1);
}

if(!arrList.Contains(obj2)) {
//THis will be called
arrList.Add(obj2);
}

if(!arrList.Contains(duplicateObj1)){
//This WON'T BE CALLED, because collection already has
//MyOBject with id 1
arrList.Add(duplicateObj1);
}

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU


"Neven Klofutar" <neven.klofutar*r.e..mo*****@vip.hr> wrote in message
news:eE**************@TK2MSFTNGP14.phx.gbl...
Nope ...

Can you tell me what must I do ... or send a useful link ?

thanx, Neven
"Teemu Keiski" <jo****@aspalliance.com> wrote in message
news:Oj**************@TK2MSFTNGP10.phx.gbl...
Have you overridden Equals method in your MyObject class to tell how it

can
detect to instances of MyObject from each other?

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
"Neven Klofutar" <neven.klofutar*r.e..mo*****@vip.hr> wrote in message
news:Ol*************@TK2MSFTNGP12.phx.gbl...
> Hi,
>
> I'm trying to use Contains method, but I have some problems ...
>
>
> // create SqlDataReader
> SqlDataReader dr = ....
>
> // create collection of myObjects
> MyCollection myObjectsColl = new MyCollection();
>
> while (dr.Read()) {
>
> // Creating myObject
> // Sometimes I create myObject using identical values of dr["a"] and
> dr["b"]
> // aren't those object equivalent ?
> MyObject myObject = Getsomething(dr["a"].ToString(),
> dr["b"].ToString());
>
> // trying to add myObject, if it's not present already in myCollection > if (!myObjectColl.Contains(myObject)) {
> myObjectsColl.Add(myObject);
> }
>
> }
>
>
> I don't know why, but (!myObjectColl.Contains(myObject)) is NEVER false
> ...
>
> please help, Neven
>
>



Nov 19 '05 #4
OK, thanx, I thing I got it to work ...

Neven
"Teemu Keiski" <jo****@aspalliance.com> wrote in message
news:OV**************@TK2MSFTNGP15.phx.gbl...
Here is an example (note that it should also overrde GetHashCode, but can
run without doing that, but couldn't be used reliably with Hashtable unless implementing that method)

Here is MyObject class which by overriding Equals makes difference between
its instances by comparing id field

===============

public class MyObject
{
//id member to represent identified for the object
private int _id;

public MyObject(int id)
{
this._id = id;
}

public override bool Equals(object obj)
{
//Test if passed object is of same type
MyObject o = obj as MyObject;
if(o != null)
{
//it is, detect if they are same object based on id member data;
return o._id == this._id;
}
return false;
}
}
==========

And here is a usage example with ArrayList comparing with Contains as in
your code

==========

//Create MyObject with ID 1
MyObject obj1=new MyObject(1);

//Create MyObject with ID 2
MyObject obj2=new MyObject(2);

//Create duplicate MyObject with ID 1
MyObject duplicateObj1=new MyObject(1);

ArrayList arrList=new ArrayList();

if(!arrList.Contains(obj1)) {
//THis will be called
arrList.Add(obj1);
}

if(!arrList.Contains(obj2)) {
//THis will be called
arrList.Add(obj2);
}

if(!arrList.Contains(duplicateObj1)){
//This WON'T BE CALLED, because collection already has
//MyOBject with id 1
arrList.Add(duplicateObj1);
}

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU


"Neven Klofutar" <neven.klofutar*r.e..mo*****@vip.hr> wrote in message
news:eE**************@TK2MSFTNGP14.phx.gbl...
Nope ...

Can you tell me what must I do ... or send a useful link ?

thanx, Neven
"Teemu Keiski" <jo****@aspalliance.com> wrote in message
news:Oj**************@TK2MSFTNGP10.phx.gbl...
Have you overridden Equals method in your MyObject class to tell how it

can
detect to instances of MyObject from each other?

--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
"Neven Klofutar" <neven.klofutar*r.e..mo*****@vip.hr> wrote in message
news:Ol*************@TK2MSFTNGP12.phx.gbl...
> Hi,
>
> I'm trying to use Contains method, but I have some problems ...
>
>
> // create SqlDataReader
> SqlDataReader dr = ....
>
> // create collection of myObjects
> MyCollection myObjectsColl = new MyCollection();
>
> while (dr.Read()) {
>
> // Creating myObject
> // Sometimes I create myObject using identical values of dr["a"] and > dr["b"]
> // aren't those object equivalent ?
> MyObject myObject = Getsomething(dr["a"].ToString(),
> dr["b"].ToString());
>
> // trying to add myObject, if it's not present already in

myCollection
> if (!myObjectColl.Contains(myObject)) {
> myObjectsColl.Add(myObject);
> }
>
> }
>
>
> I don't know why, but (!myObjectColl.Contains(myObject)) is NEVER false > ...
>
> please help, Neven
>
>



Nov 19 '05 #5

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

Similar topics

3
by: Jens Stjärna | last post by:
Hi. I have a question regarding the ArrayList. In my code I use the arraylist to store objects of certain class. I do not mix object types in the same ArrayList. public ArrayList adresses = new...
9
by: TT ( Tom Tempelaere ) | last post by:
Hi At one point in my application I have a single ArrayList object that I need to break up in two Arraylist objects: the beginning part up to an index, and the ending part from a certain index. I...
4
by: Hans De Schrijver | last post by:
I have a private ArrayList variable that holds objects of various types, though they're all derived from a common base class (User). What I would like to do is provide public accessor properties...
0
by: Just D. | last post by:
There is an interesting article - http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=542&printer=t It shows how to serialize the ArrayList of identical objects. I did it a year...
19
by: Derek Martin | last post by:
Hi there, I have been playing with sorting my arraylist and having some troubles. Maybe just going about it wrong. My arraylist contains objects and one of the members of the object is 'name.' I...
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...
16
by: RCS | last post by:
So I have an ArrayList that gets populated with objects like: myAL.Add(new CustomObject(parm1,parm2)); I'm consuming this ArrayList from an ObjectDataSource and would like to have this support...
14
by: budy_ludy | last post by:
Hi All, I am new to vb .net, I have an ArrayList and i store class objects in it, and later i want to retrieve each ArrayList items and type cast to the class, How can it be done ? I used...
14
by: Kym | last post by:
Hey, I have an arraylist which I have added a number of objects to (all the same type of object), I need to be able to check if the array already contain the object before adding it, I have tried...
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?
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...
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
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
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
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
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...

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.