473,545 Members | 2,291 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.
GenericKeyedCol lection<int, Person> collection = new
GenericKeyedCol lection<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.Coun t);
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 GenericKeyedCol lection.
using System;
using System.Collecti ons.Generic;
using System.Collecti ons.ObjectModel ;
using System.Text;
namespace Unity.Domain
{
public class GenericKeyedCol lection<Key, Type> :
KeyedCollection <Key,
Type>
{
protected override Key GetKeyForItem(T ype item)
{
//What do I put here since at runtime
//I don't know what the

type will be?
}
}

Dec 12 '05 #1
10 4380
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.GetK eyForItem(perso n1).;

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.goo glegroups.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.
GenericKeyedCol lection<int, Person> collection = new
GenericKeyedCol lection<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.Coun t);
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 GenericKeyedCol lection.
using System;
using System.Collecti ons.Generic;
using System.Collecti ons.ObjectModel ;
using System.Text;
namespace Unity.Domain
{
public class GenericKeyedCol lection<Key, Type> :
KeyedCollection <Key,
Type>
{
protected override Key GetKeyForItem(T ype 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 PersonCollectio n : KeyedCollection <int, Person>
{
public PersonCollectio n() : base() { }

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

PersonCollectio n pc = new PersonCollectio n();
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 "GenericKeyedCo llection" 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 GenericKeyedCol lection<int, Chapter> chapters = new
GenericKeyedCol lection<int, Chapter>();

}
public class Shelf : DomainBase
{
//Use string as key becuase we will be using the ISBN
private GenericKeyedCol lection<string, Book> books = new
GenericKeyedCol lection<string, Book>();
}
That way I would be able to use the GenericKeyColle ction 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.goo glegroups.com.. .
You suggestion would work however I would lose the generic
instantiation of the "GenericKeyedCo llection" 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 GenericKeyedCol lection<int, Chapter> chapters = new
GenericKeyedCol lection<int, Chapter>();

}
public class Shelf : DomainBase
{
//Use string as key becuase we will be using the ISBN
private GenericKeyedCol lection<string, Book> books = new
GenericKeyedCol lection<string, Book>();
}
That way I would be able to use the GenericKeyColle ction 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 GenericKeyedCol lection<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

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

Similar topics

5
2515
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 "Enlighten.LinkMad.Businesslogic". In one of my frontend websites I use this type to authenticate a user who is trying to login. The following excerpt is from the...
2
2567
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 good while and I'm getting really frustrated now! My problem is this - my custom controls periodically disappear from my
27
45504
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 height as they were in previous versions or will they be the "ribbon" style that takes up a huge portion of the screen? Also when I use Access...
15
6483
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 accept other controls. The control i drag drop on it becomes the child of my custom control's parent form and not the child of my custom control. Then...
2
19415
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 will be writing this article intended for those who are in the same level, or maybe lower, of my technical knowledge. I would be using layman's...
1
4369
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 grid doesn't show colloections i.e. if I have a List<Objectthen the Collection is shown but when I click on the "..." button next to it nothing...
0
2897
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 will be writing this article intended for those who are in the same level, or maybe lower, of my technical knowledge. I would be using layman's...
0
7465
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7398
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...
0
7656
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. ...
0
7805
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...
0
7752
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5325
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3449
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...
0
3441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
701
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.