473,320 Members | 2,003 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,320 software developers and data experts.

Custom KeyCollection

I am trying to create a custom KeyCollection that will only allow my
domain objects to be added to the Collection. I was hoping I could
achieve the following implementation.
GenericKeyedCollection<int, Person> collection = new
GenericKeyedCollection<int, Person>();
Person person1 = new Person();
person1.ID = 1;
person1.Name = "Jimmy";
Person person2 = new Person();
person2.ID = 2;
person2.Name = "Chris";
collection.Add(person1);
collection.Add(person2);
Assert.AreEqual(2, collection.Count);
Now I thought I understood how to do this but I don't know what to
put in the absratct protected override GetKeyForItem. Can someone
please give me some guidance on what to put here?
Here is my implmentation of the GenericKeyedCollection.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
namespace Unity.Domain
{
public class GenericKeyedCollection<Key, Type> :
KeyedCollection<Key,
Type>
{
protected override Key GetKeyForItem(Type item)
{
//What do I put here since at runtime
//I don't know what the

type will be?
}
}

Dec 12 '05 #1
10 4374
IMHO you have 2 options:

return the key that satisfies the value ( find value in the list that is
equal to item and another may be auto-key generation for example

Person person1 = new Person();
person1.Name = "Jimmy";
person1.ID = collection.GetKeyForItem(person1).;

You can return either incremented value, hashcode, or some calculated value

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

"JoSkillz" <oc*****@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
I am trying to create a custom KeyCollection that will only allow my
domain objects to be added to the Collection. I was hoping I could
achieve the following implementation.
GenericKeyedCollection<int, Person> collection = new
GenericKeyedCollection<int, Person>();
Person person1 = new Person();
person1.ID = 1;
person1.Name = "Jimmy";
Person person2 = new Person();
person2.ID = 2;
person2.Name = "Chris";
collection.Add(person1);
collection.Add(person2);
Assert.AreEqual(2, collection.Count);
Now I thought I understood how to do this but I don't know what to
put in the absratct protected override GetKeyForItem. Can someone
please give me some guidance on what to put here?
Here is my implmentation of the GenericKeyedCollection.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
namespace Unity.Domain
{
public class GenericKeyedCollection<Key, Type> :
KeyedCollection<Key,
Type>
{
protected override Key GetKeyForItem(Type item)
{
//What do I put here since at runtime
//I don't know what the

type will be?
}
}

Dec 12 '05 #2
You need to return the unique key:

public class PersonCollection : KeyedCollection<int, Person>
{
public PersonCollection() : base() { }

protected override int GetKeyForItem(Person person)
{
return person.ID;
}
}

PersonCollection pc = new PersonCollection();
pc.Add(new Person(1, "Gabriel", "Lozano-Morán");

Gabriel Lozano-Morán
MCSD .NET
Real Software
http://www.realdn.net
http://www.realsoftware.be
Dec 12 '05 #3
You suggestion would work however I would lose the generic
instantiation of the "GenericKeyedCollection" class from a generics
perspective. I want to be able to declare the following private field
declarations.
public class Chapter : DomainBase {}
public class Book : DomainBase
{
private GenericKeyedCollection<int, Chapter> chapters = new
GenericKeyedCollection<int, Chapter>();

}
public class Shelf : DomainBase
{
//Use string as key becuase we will be using the ISBN
private GenericKeyedCollection<string, Book> books = new
GenericKeyedCollection<string, Book>();
}
That way I would be able to use the GenericKeyCollection with ANY
object in the future and rely on the power of Generics to work for me.

Dec 12 '05 #4
One way to solve this is to make sure that the generic part, like Chapter or
Person implement an interface for example:

public interface IKeyed
{
int Key { get; }
}

Then add the generic constraint where T is IKeyed and in the GetKeyForItem()
return the Key property.

Gabriel

"JoSkillz" <oc*****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
You suggestion would work however I would lose the generic
instantiation of the "GenericKeyedCollection" class from a generics
perspective. I want to be able to declare the following private field
declarations.
public class Chapter : DomainBase {}
public class Book : DomainBase
{
private GenericKeyedCollection<int, Chapter> chapters = new
GenericKeyedCollection<int, Chapter>();

}
public class Shelf : DomainBase
{
//Use string as key becuase we will be using the ISBN
private GenericKeyedCollection<string, Book> books = new
GenericKeyedCollection<string, Book>();
}
That way I would be able to use the GenericKeyCollection with ANY
object in the future and rely on the power of Generics to work for me.

Dec 12 '05 #5
How do I add a constraint to the generic to be a certain type? because
in that case I can declare the type to always be of type DomainBase.

Dec 12 '05 #6
Eg:

public class GenericKeyedCollection<Key, Type> : KeyedCollection<Key, Type>
where Type : IKeyed

Gabriel
Dec 13 '05 #7
LOL...bluring the lines between SQL and C# now we have where clauses

Dec 13 '05 #8
I guess you are not familiar yet with Project LINQ ? :-)
http://msdn.microsoft.com/netframewo...q/default.aspx

Gabriel
Dec 13 '05 #9
Just when I just starting to wrap my hands around HQL and Generics they
do this! Geesh.. I need to upgrade my brain it's becoming outdated.

Dec 13 '05 #10
I know it's hard to keep up. And then if you see what profiles employers
require, it's becoming impossible. I used to be a contractor but it was
really hard to get projects because being a contractor they expected from
you that you know everything.

Gabriel
Dec 14 '05 #11

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

Similar topics

5
by: Graham | last post by:
I have created a custom MembershipProvider called "LassieMembershipProvider" that derives from "MembershipProvider". This providor is located in a Businesslogic layer dll called...
2
by: Suzanne | last post by:
Hi all, I'm reposting this message as I'm experiencing this problem more and more frequently : I really hope someone out there can help me as I've been tearing my hair out on this one for a...
27
by: Wayne | last post by:
I've been clicking around Access 2007 Beta 2 and can't see the custom menu bar designer. Is it in the beta? Maybe I'm blind. The question that comes to mind is: Will custom menu bars be the same...
15
by: rizwanahmed24 | last post by:
Hello i have made a custom control. i have placed a panel on it. I want this panel to behave just like the normal panel. The problem i was having is that the panel on my custom control doesnt...
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
1
by: asharda | last post by:
I have a custom property grid. I am using custom property grid as I do not want the error messages that the propertygrid shows when abphabets are entered in interger fields. The custom property...
0
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shćllîpôpď 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.