473,888 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(myObje ct)) {
myObjectsColl.A dd(myObject);
}

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

please help, Neven
Nov 19 '05 #1
4 1198
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*****@v ip.hr> wrote in message
news:Ol******** *****@TK2MSFTNG P12.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(myObje ct)) {
myObjectsColl.A dd(myObject);
}

}
I don't know why, but (!myObjectColl. Contains(myObje ct)) 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****@aspalli ance.com> wrote in message
news:Oj******** ******@TK2MSFTN GP10.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*****@v ip.hr> wrote in message
news:Ol******** *****@TK2MSFTNG P12.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(myObje ct)) {
myObjectsColl.A dd(myObject);
}

}
I don't know why, but (!myObjectColl. Contains(myObje ct)) 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=n ew MyObject(1);

ArrayList arrList=new ArrayList();

if(!arrList.Con tains(obj1)) {
//THis will be called
arrList.Add(obj 1);
}

if(!arrList.Con tains(obj2)) {
//THis will be called
arrList.Add(obj 2);
}

if(!arrList.Con tains(duplicate Obj1)){
//This WON'T BE CALLED, because collection already has
//MyOBject with id 1
arrList.Add(dup licateObj1);
}

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


"Neven Klofutar" <neven.klofutar *r.e..mo*****@v ip.hr> wrote in message
news:eE******** ******@TK2MSFTN GP14.phx.gbl...
Nope ...

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

thanx, Neven
"Teemu Keiski" <jo****@aspalli ance.com> wrote in message
news:Oj******** ******@TK2MSFTN GP10.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*****@v ip.hr> wrote in message
news:Ol******** *****@TK2MSFTNG P12.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(myObje ct)) {
> myObjectsColl.A dd(myObject);
> }
>
> }
>
>
> I don't know why, but (!myObjectColl. Contains(myObje ct)) is NEVER false
> ...
>
> please help, Neven
>
>



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

Neven
"Teemu Keiski" <jo****@aspalli ance.com> wrote in message
news:OV******** ******@TK2MSFTN GP15.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=n ew MyObject(1);

ArrayList arrList=new ArrayList();

if(!arrList.Con tains(obj1)) {
//THis will be called
arrList.Add(obj 1);
}

if(!arrList.Con tains(obj2)) {
//THis will be called
arrList.Add(obj 2);
}

if(!arrList.Con tains(duplicate Obj1)){
//This WON'T BE CALLED, because collection already has
//MyOBject with id 1
arrList.Add(dup licateObj1);
}

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


"Neven Klofutar" <neven.klofutar *r.e..mo*****@v ip.hr> wrote in message
news:eE******** ******@TK2MSFTN GP14.phx.gbl...
Nope ...

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

thanx, Neven
"Teemu Keiski" <jo****@aspalli ance.com> wrote in message
news:Oj******** ******@TK2MSFTN GP10.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*****@v ip.hr> wrote in message
news:Ol******** *****@TK2MSFTNG P12.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(myObje ct)) {
> myObjectsColl.A dd(myObject);
> }
>
> }
>
>
> I don't know why, but (!myObjectColl. Contains(myObje ct)) 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
13955
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 ArrayList(); I store only objects from the class "CAdresses" in this ArrayList. Now, every time I use the objects in the ArrayList I have to type cast them into aCAdresses class type object since the compiler only knows it is a
9
4320
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 am now using ArrayList.GetRange but the documentation states that this method does not create new ArrayList objects but views into the source ArrayList object ArrayList original // .. ArrayList l1 = original.GetRange( 0, count ) ArrayList l2 =...
4
1722
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 per type. I have written some code that does the trick now, but it involves looping through the private ArrayList and creating a new Array with just the objects of the type corresponding to the property. Problem is, this hapens every time you...
0
7300
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 ago and it works fine. The question is if the ArrayList has a set of different objects of different type, every time different. We know what types should be involved, but we don't know what objects in what order and how many are used in the...
19
2474
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 would like to sort the arraylist based on object.name - is that possible? Thanks! Derek
6
1469
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 example I found on the web. I would be most appreciative if someone could explain why this statement is necessary and what does it do: MyArt = New Art
18
4767
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 class where only the "searched" property has a value. I expected to get the index into the arraylist where I could then get the entire class instance. However, the 'indexof' is never calling my overloaded, overrides Equals method. Here is the...
16
6440
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 sorting (because it's ultimately being consumed in a GridView). I can't simply sort the ArrayList (because it only knows it's holding a bunch of objects). So I need a way to sort the ArrayList, based on the data - that is within the objects that...
14
18766
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 CType for casting but it is throwing exception.
14
1969
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 something like... if Not myArray.contains(newObject) then myArray.Add(newObject) end if This will work with a string object but not a user defined object.
0
9800
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11185
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10778
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10887
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9597
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5824
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6014
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4642
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 we have to send another system
2
4247
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.