Hello,
I have a class that inherits from the CollectionBase class that contains
records from a table in database that has a compound key.

Items Table
Company_ID [PK, int]
Item_ID [PK, int]
Description [varchar(50)]

Does anyone have any suggestions on how to implement a property, similar to
Item(index as Integer), that accepts the compound keys?

Desired Property
Item(company_id As Integer, item_id as Integer) As CompanyItem

Currently if the table has only a primary key, I create a hashtable to store
the primary key and the index of the object.

Private m_htKeys As Hashtable

Default Public Property Item(ByVal key As String) As CompanyItem
Get
Return list.Item(m_htKeys.Item(key))
End Get
Set(ByVal Value As CompanyItem)
list.Item(m_htKeys.Item(key)) = Value
End Set
End Property

I have tried storing the compound keys as a String Array in the hashtable,
however when I try to find that String Arrary again it just returns the first
object.

Is there a better way of going about this? Am I crazy?

Justin

Re: Collection Class Design Question (Compound Key) by Matt

Matt
Thu Mar 24 14:08:51 CST 2005

Hello JustinS,

A few ways you could to this:

Do a string concatenation when you create your compound key and use that
as the key to the hashtable.

[C#]
public CompanyItem this[int companyId, int itemId]
{
get
{
string key = companyId.ToString() + "||" + itemId.ToString();
return items[key];
}
set
{
string key = companyId.ToString() + "||" + itemId.ToString();
items[key] = value;
}

Another way is to implement your compound key as a class:

[C#]
public class CompanyKey
{
public readonly int Id;
public readonly int ItemId;

public CompanyKen(int id, int itemId)
{
this.Id = id;
this.ItemId = itemId;
}
}

public CompanyItem this[CompanyKey]
{
get { return items[key]; }
set { items[key] = value; }
}

Lastly, it appears that you're storing your key and your values separately
(ie: list and m_htKeys). Perhaps a better class to derive from is DictionaryBase.
This lends itself very nicely to the key/value paradigm that you're working
with.

--
Matt Berther
http://www.mattberther.com

> Hello,
> I have a class that inherits from the CollectionBase class that
> contains
> records from a table in database that has a compound key.
> Items Table
> Company_ID [PK, int]
> Item_ID [PK, int]
> Description [varchar(50)]
> Does anyone have any suggestions on how to implement a property,
> similar to Item(index as Integer), that accepts the compound keys?
>
> Desired Property
> Item(company_id As Integer, item_id as Integer) As CompanyItem
> Currently if the table has only a primary key, I create a hashtable to
> store the primary key and the index of the object.
>
> Private m_htKeys As Hashtable
>
> Default Public Property Item(ByVal key As String) As CompanyItem
> Get
> Return list.Item(m_htKeys.Item(key))
> End Get
> Set(ByVal Value As CompanyItem)
> list.Item(m_htKeys.Item(key)) = Value
> End Set
> End Property
> I have tried storing the compound keys as a String Array in the
> hashtable, however when I try to find that String Arrary again it just
> returns the first object.
>
> Is there a better way of going about this? Am I crazy?
>
> Justin
>