Hashtable question

  • Thread starter Thread starter MFRASER
  • Start date Start date
M

MFRASER

I am trying to create a collection object that contains an internal
hashtable to help in retrieving data, but what I have relized is that I am
adding the object multiple times is there a way to add a key that points to
the reference of the object or do I not understand the hashtable?

Here is the reasoning behind my madness, I wanted a collection that the user
could enumerate through using the foreach, I also wanted a collection that
was quick to retrieve data.

Here is my code

public class MyDateCollection: ICollectionBase
{
private System.Collections.Hashtable m_IternalCollection =
new Hashtale();
...
//Here is my add method

public void Add(MyDate Item)
{
//Add to internal Hashtable
if(!this.m_IternalCollection.Contains(Item.ItemDate)
this.m_IternalCollection.Add(Item.ItemDate, Item);
if(!this.m_IternalCollection.Contains("Day" + Item.ItemDate.Date)
this.m_IternalCollection.Add("Day" + Item.ItemDate.Date, Item);
//Add to Collection
base.InnerList.Add(Item);
}

public MyDate this[System.DateTime TestDate, bool DayOnly]
{
get
{
if (DayOnly)
return (MyDate)this.m_IternalCollection["Day" +
TestDate.Date];
else
return (MyDate)this.m_IternalCollection[TestDate];
}

}

}
 
MFRASER said:
I am trying to create a collection object that contains an internal
hashtable to help in retrieving data, but what I have relized is that
I am adding the object multiple times is there a way to add a key
that points to the reference of the object or do I not understand the
hashtable?

I think it's more likely that you don't understand reference types,
although that depends on exactly what MyDate is. If it's a class, don't
worry - you're adding two references to the same object, not creating
copies of the object itself.

I'd suggest having two different hashtables, however - one for the
dates and one for dates/times, rather than prepending "Day" to the
date.
 
Back
Top