473,788 Members | 2,694 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generics Dictionary - modify ContainsKey and Key Lookup

Hi

I have a class that inherits from Generics Dictionary
The string that is used for the key is passed thru-out my pgm and sometimes
it has modifiers added to the key string that are used in the system.

The problem is now I have to strip the modifer to lookup in the Dictionary
and I have to copy this code whenever I need to lookup the key or if more
modifers are added

Is there someway for me to override the way the Dictionary Lookups keys
so I can write the code to strip the modifer in one place so when I do a
ContainsKey
of this[sFileName][sKey] - if will look for key and if not found try to find
again after replace????

Thanks

if (this[sFileName].ContainsKey(sK ey))
{
return (this[sFileName][sKey].Info);
}
else if (this[sFileName].ContainsKey(sK ey.Replace("#", "")))
{
return (this[sFileName][sKey.Replace("# ","")]Info);
}
else if (this[sFileName].ContainsKey(sK ey.Replace("^", "")))
{
return (this[sFileName][sKey.Replace("^ ","")]Info);
}
Oct 29 '07 #1
2 3449
You can implement the IEqualityCompar er<Tinterface, implementing the
Equals and GetHashCode methods to work on the modified keys for comparison,
and then pass that implementation to the dictionary.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"sippyuconn " <si********@new sgroup.nospamwr ote in message
news:85******** *************** ***********@mic rosoft.com...
Hi

I have a class that inherits from Generics Dictionary
The string that is used for the key is passed thru-out my pgm and
sometimes
it has modifiers added to the key string that are used in the system.

The problem is now I have to strip the modifer to lookup in the Dictionary
and I have to copy this code whenever I need to lookup the key or if more
modifers are added

Is there someway for me to override the way the Dictionary Lookups keys
so I can write the code to strip the modifer in one place so when I do a
ContainsKey
of this[sFileName][sKey] - if will look for key and if not found try to
find
again after replace????

Thanks

if (this[sFileName].ContainsKey(sK ey))
{
return (this[sFileName][sKey].Info);
}
else if (this[sFileName].ContainsKey(sK ey.Replace("#", "")))
{
return (this[sFileName][sKey.Replace("# ","")]Info);
}
else if (this[sFileName].ContainsKey(sK ey.Replace("^", "")))
{
return (this[sFileName][sKey.Replace("^ ","")]Info);
}

Oct 29 '07 #2
Hi,

Have you reviewed Nicholas Paldino [.NET/C# MVP]'s reply to you? Does it
resolve your question? If you still need any help, please feel free to
feedback, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.
Nov 5 '07 #3

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

Similar topics

5
1792
by: Brian Richards | last post by:
I'm experiencing some wierd behavior with a Dictionary<T,U> class using foreach loops, such that the Key returned in the foreach is not contained in the dictionary. code: Dictionary<A, B> dict; ..... foreach(A key in dict.Keys) {
0
1547
by: Frank | last post by:
I'm using VB in ASP.NET 2.0. I've created a dictionary with a custom key that contains 2 integers as properties. The key is in the dictionary, but when I do a ContainsKey(mykey) it always returns false. This works if the key in the dictionary is just a single integer. I a C# forum I found a reference to overriding an objects GetHashCode. Because the ContainsKey. Anyone have an example of how to do this? Is this the right approach? ...
3
1602
by: craigkenisston | last post by:
I want to use Dictionary class like this: textboxM.Text = oMyDict.GetValue("textboxM"); textboxN.Text = oMyDict.GetValue("textboxN"); Where my dictionary contains the list of values for my textbox based on the textbox id. But it doesn't have it !
5
1943
by: Stefano Peduzzi | last post by:
Hi, I'm building an application where I've defined a custom class Customer. Customer can have many phones (defined by phoneType and phoneNumber). I want to check that a phoneNumber is not already present in customer phones. So i've build a PhoneCollection class inheriting for List<Phoneand defined an Add method to check if the phoneNumber already exists. Then I added the PhoneCollection to Customer properties. Everything works fine, and I...
0
1323
by: xpding | last post by:
Hello, I have a class MyEmbededList contains a generic dictionary, the value field is actually the MyEmbededList type as well. There is another class need to access and manipulate a list of MyEmbededList (please refer to the MyTestClass below). I am not sure whether I implements the right locking mechanism here and hope someone can give me some advices. I have provided some codes for these two classes below. My questions are: 1. Am I...
18
4298
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I've a question regarding dictionaries. I have an array elements that I created, and I'm trying to sort those elements into various sections. Here's the template of my data type. DT { int nSize; ...
3
2019
by: Phil Sandler | last post by:
All, I have a situation where I need a List<stringthat performs like a generic dictionary (Dictionary<string, string>). Essentially, I just need the key, not the value, so I want to use as little memory as possible. Currently I am setting all the values of the dictionary to null. The List<stringdoesn't work because I am doing a lot of .Contains() or .ContainsKey(), and lookup speed is pretty critical.
8
7996
by: Bob Altman | last post by:
Hi all, I'm trying to do something that should be really easy, but I can't think of an obvious way to do it. I have a dictionary whose value is a "value type" (as opposed to a reference type -- in this case, a Boolean): Dim dic As New Dictionary(Of Int32, Boolean) dic(123) = True I want to set all of the existing entries in the dictionary to False. The
3
1269
by: Dmitry Nogin | last post by:
Hi, I would like to use dictionaries to implement my property value storing mechanics. (I have a lot but many of them are just about to keep <nullmost of the time, so it might save a lot of memory.) So, my solution is: class Person {
0
9498
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10370
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10177
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8995
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5402
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4074
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2896
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.