i have optimised the code by removing
public int Add(CrapObj val) in the BreatheArrayList class i am using casting to do the same, if u need explanations for the code plz let me know
-----------------start-----------------
using System;
using System.Collections;
/// <summary>
/// Summary description for Class1.
/// </summary>
public class CrapObj
{
int i, j;
string strName;
public CrapObj(int i, int j, string strName)
{
this.i = i;
this.j = j;
this.strName = strName;
}
public void DisplayCrap()
{
Console.WriteLine("i: "+i.ToString()+" ,j: "+j.ToString()+" ,name: "+strName);
}
public bool Equals(CrapObj obj)
{
if(obj.i == this.i && obj.j == this.j && obj.strName == this.strName)
return true;
return false;
}
}
public class BreatheArrayList:ArrayList
{
/// <summary>
/// Going strong
/// </summary>
public BreatheArrayList()
{
}
public override int Add(object val)
{
CrapObj obj = (CrapObj) val;
if(this.Count!=0)
{
for(int i = 0;i<this.Count;i++)
{
if(obj.Equals((CrapObj)this[i]))
{
return -1;
}
}
}
return base.Add (val);
}
}
class CheckWhetherItBreathes
{
public CheckWhetherItBreathes()
{
}
public static void Main()
{
CrapObj obj1 = new CrapObj(3,1,"obj1");
CrapObj obj2 = new CrapObj(2,1,"obj2");
CrapObj obj3 = new CrapObj(1,1,"obj3");
CrapObj duplicate = new CrapObj(3,1,"obj1");
BreatheArrayList obj = new BreatheArrayList();
obj.Add(obj1);
obj.Add(obj2);
obj.Add(obj3);
obj.Add(duplicate);
for(int i =0; i < obj.Count; i++)
{
CrapObj objCheck = (CrapObj)obj[i];
objCheck.DisplayCrap();
}
}
}
-----------------End-----------------
--
Do not mistake that i am crazy bout .net it is just that it is filled with so many bugs that it ends up in u cleaning up the mess day and night.
And hence u breathe .net all the time.
"breathedotnet" wrote:
Here is the code as promised, object oriented concepts are gr8 dont u agree
using System;
using System.Collections;
/// <summary>
/// Summary description for Class1.
/// </summary>
public class CrapObj
{
int i, j;
string strName;
public CrapObj(int i, int j, string strName)
{
this.i = i;
this.j = j;
this.strName = strName;
}
public void DisplayCrap()
{
Console.WriteLine("i: "+i.ToString()+" ,j: "+j.ToString()+" ,name: "+strName);
}
public bool Equals(CrapObj obj)
{
if(obj.i == this.i && obj.j == this.j && obj.strName == this.strName)
return true;
return false;
}
}
public class BreatheArrayList:ArrayList
{
/// <summary>
/// Going strong
/// </summary>
public BreatheArrayList()
{
}
public int Add(CrapObj val)
{
if(this.Count!=0)
{
for(int i = 0;i<this.Count;i++)
{
if(val.Equals((CrapObj)this[i]))
{
return -1;
}
}
}
return Add((object)val);
}
public override int Add(object val)
{
return base.Add (val);
}
}
class CheckWhetherItBreathes
{
public CheckWhetherItBreathes()
{
}
public static void Main()
{
CrapObj obj1 = new CrapObj(3,1,"obj1");
CrapObj obj2 = new CrapObj(2,1,"obj2");
CrapObj obj3 = new CrapObj(1,1,"obj3");
CrapObj duplicate = new CrapObj(3,1,"obj1");
BreatheArrayList obj = new BreatheArrayList();
obj.Add(obj1);
obj.Add(obj2);
obj.Add(obj3);
obj.Add(duplicate);
for(int i =0; i < obj.Count; i++)
{
CrapObj objCheck = (CrapObj)obj[i];
objCheck.DisplayCrap();
}
}
}
--
Do not mistake that i am crazy bout .net it is just that it is filled with so many bugs that it ends up in u cleaning up the mess day and night.
And hence u breathe .net all the time.
"breathedotnet" wrote:
Hey try overriding the ArrayList,HashTable function Add method when u are adding objects into the collection in this overridden method check for the whether the object already exists. I will write this code for you, in the mean time even u try doing the same
--
Do not mistake that i am crazy bout .net it is just that it is filled with so many bugs that it ends up in u cleaning up the mess day and night.
And hence u breathe .net all the time.
"Sean Hearne" wrote:
I have numerous collections (currently ArrayLists) which contain
objects of the same type. I need some way of aggregating these lists
into one collection with duplicate objects only appearing once in the
final collection (set type collection).
Currently i'm using an Hashtable, entering the object as key while
checking beforehand if the key exists (avoid duplicates).
Whats the best way to do this? The technique should be as efficient as
possible as it is contained within a block of code that loops
frequently.