Collection

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

I have a collection of Test Class with the following keys

Class Test
{
public int num1;
public int num2;
}

Key1 = "1"
Key2 = "1"
Key3 = "1"
Key4 = "2"
Key5 = "2"
Key6 = "2"
Key7 = "3"
Key8 = "3"
Key9 = "3"


I want to change num1 to equal 100 if the Key is "2".

How do I get to all objects where key = "2" without iterating through all of
the keys.


Thanks


Peter
 
Peter,

You have a lot of option. The first one off the top of my head is
store the objects with like keys in an ArrayList and then put the
ArrayList in a Hashtable. The code to retrieve the objects might look
like the following.

ArrayList list = hashtable[2] as ArrayList;
if (list != null)
{
foreach (Test test in list)
{
test.num1 = 100;
}
}

Brian
 
Hi Peter,

You can use a HashTable instead of a collection of Test class, in this
case. In the HashTable, you will be able to find value through key
directly. It's fast than any kind of arrays.

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
The Hashtable by itself isn't sufficient because it won't allow you
store more than one object with the same key.
 
Hi Oops,

Sorry, I mistook the num1 for keys. In this case, we can use Test class
objects as values.

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Kevin Yu said:
Hi Oops,

Sorry, I mistook the num1 for keys. In this case, we can use Test class
objects as values.

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Thank you very much, I think this will work.
 
Back
Top