Implementing a key/value pair collection...with a catch | | |
I have an interesting problem and I'm not coming up with any answers in my
searches, so hopefully someone can give me a hand with this. I have a
feeling it's easy, but I usually get my nose stuck too far in the details to
see the big picture. :)
I want to create a collection of key/value pairs. I have a class for the
key/value pair...we'll just call it KeyValuePair for this example. Now,
when creating the collection, I need to be able to have duplicate
keys...this is why I can't just use a hashtable. One more requirement is
that I can iterate through the entire collection, or just a subset of the
collection based on a key. The final requirement is that all key/value
pairs stay in the order that I added them...I can't have them rearranged
when I iterate through them. So, let's say the following is the data stored
in the collection:
Key Value
------------
AAA Test 1
BBB Test 2
AAA Test 3
BBB Test 4
AAA Test 5
CCC Test 6
DDD Test 7
EEE Test 8
EEE Test 9
I'd like to be able to do this:
For Each keyValuePair in keyValuePairCollection.Items
Debug.WriteLine("The key is " & keyValuePair.Key & " and the value is "
& keyValuePair.Value & ".")
keyValuePair.Value = keyValuePair.Value & " has been modified"
Debug.WriteLine("The new value is " & keyValuePair.Value & ".")
Next
The above code would iterate through all 9 key/value pairs and modify their
values. I'd also like to be able to do this:
For Each keyValuePair in KeyValuePairCollection.FilteredItems("AAA")
Debug.WriteLine("The key is " & keyValuePair.Key & " and the value is "
& keyValuePair.Value & ".")
keyValuePair.Value = keyValuePair.Value & " has been modified"
Debug.WriteLine("The new value is " & keyValuePair.Value & ".")
Next
The above code would iterate through only the 3 key/value pairs with a key
of "AAA" and modify their values. So...is all of this possible? Can anyone
send me in the right direction? I'd really appreciate any help you can
give. Thanks! | | | | re: Implementing a key/value pair collection...with a catch
Hi,
Here is a suggestion. You store any type of data in a hashtable.
Why not store an arraylist with all of your values for each key.
Ken
-----------------------
"Jeff L." <nospam@nospam.com> wrote in message
news:%23NATeCQ4DHA.2480@TK2MSFTNGP09.phx.gbl...[color=blue]
>I have an interesting problem and I'm not coming up with any answers in my
> searches, so hopefully someone can give me a hand with this. I have a
> feeling it's easy, but I usually get my nose stuck too far in the details
> to
> see the big picture. :)
>
> I want to create a collection of key/value pairs. I have a class for the
> key/value pair...we'll just call it KeyValuePair for this example. Now,
> when creating the collection, I need to be able to have duplicate
> keys...this is why I can't just use a hashtable. One more requirement is
> that I can iterate through the entire collection, or just a subset of the
> collection based on a key. The final requirement is that all key/value
> pairs stay in the order that I added them...I can't have them rearranged
> when I iterate through them. So, let's say the following is the data
> stored
> in the collection:
>
> Key Value
> ------------
> AAA Test 1
> BBB Test 2
> AAA Test 3
> BBB Test 4
> AAA Test 5
> CCC Test 6
> DDD Test 7
> EEE Test 8
> EEE Test 9
>
> I'd like to be able to do this:
>
> For Each keyValuePair in keyValuePairCollection.Items
> Debug.WriteLine("The key is " & keyValuePair.Key & " and the value is "
> & keyValuePair.Value & ".")
> keyValuePair.Value = keyValuePair.Value & " has been modified"
> Debug.WriteLine("The new value is " & keyValuePair.Value & ".")
> Next
>
> The above code would iterate through all 9 key/value pairs and modify
> their
> values. I'd also like to be able to do this:
>
> For Each keyValuePair in KeyValuePairCollection.FilteredItems("AAA")
> Debug.WriteLine("The key is " & keyValuePair.Key & " and the value is "
> & keyValuePair.Value & ".")
> keyValuePair.Value = keyValuePair.Value & " has been modified"
> Debug.WriteLine("The new value is " & keyValuePair.Value & ".")
> Next
>
> The above code would iterate through only the 3 key/value pairs with a key
> of "AAA" and modify their values. So...is all of this possible? Can
> anyone
> send me in the right direction? I'd really appreciate any help you can
> give. Thanks!
>
>[/color] | | | | re: Implementing a key/value pair collection...with a catch
I think I get what you're saying...the problem is that if I do it that way,
the key/value pairs will no longer be correctly ordered. I need to keep
them in their original order (which can be any order at all...no specific
sorting or anything). If I stick them into a hashtable based on their key
value, they won't be ordered correctly any more.
Jeff
"Ken Tucker [MVP]" <vb2ae@bellsouth.net> wrote in message
news:Om$UOPQ4DHA.1052@TK2MSFTNGP12.phx.gbl...[color=blue]
> Hi,
>
> Here is a suggestion. You store any type of data in a hashtable.
> Why not store an arraylist with all of your values for each key.
>
> Ken
> -----------------------
> "Jeff L." <nospam@nospam.com> wrote in message
> news:%23NATeCQ4DHA.2480@TK2MSFTNGP09.phx.gbl...[color=green]
> >I have an interesting problem and I'm not coming up with any answers in[/color][/color]
my[color=blue][color=green]
> > searches, so hopefully someone can give me a hand with this. I have a
> > feeling it's easy, but I usually get my nose stuck too far in the[/color][/color]
details[color=blue][color=green]
> > to
> > see the big picture. :)
> >
> > I want to create a collection of key/value pairs. I have a class for[/color][/color]
the[color=blue][color=green]
> > key/value pair...we'll just call it KeyValuePair for this example. Now,
> > when creating the collection, I need to be able to have duplicate
> > keys...this is why I can't just use a hashtable. One more requirement[/color][/color]
is[color=blue][color=green]
> > that I can iterate through the entire collection, or just a subset of[/color][/color]
the[color=blue][color=green]
> > collection based on a key. The final requirement is that all key/value
> > pairs stay in the order that I added them...I can't have them rearranged
> > when I iterate through them. So, let's say the following is the data
> > stored
> > in the collection:
> >
> > Key Value
> > ------------
> > AAA Test 1
> > BBB Test 2
> > AAA Test 3
> > BBB Test 4
> > AAA Test 5
> > CCC Test 6
> > DDD Test 7
> > EEE Test 8
> > EEE Test 9
> >
> > I'd like to be able to do this:
> >
> > For Each keyValuePair in keyValuePairCollection.Items
> > Debug.WriteLine("The key is " & keyValuePair.Key & " and the value is[/color][/color]
"[color=blue][color=green]
> > & keyValuePair.Value & ".")
> > keyValuePair.Value = keyValuePair.Value & " has been modified"
> > Debug.WriteLine("The new value is " & keyValuePair.Value & ".")
> > Next
> >
> > The above code would iterate through all 9 key/value pairs and modify
> > their
> > values. I'd also like to be able to do this:
> >
> > For Each keyValuePair in KeyValuePairCollection.FilteredItems("AAA")
> > Debug.WriteLine("The key is " & keyValuePair.Key & " and the value is[/color][/color]
"[color=blue][color=green]
> > & keyValuePair.Value & ".")
> > keyValuePair.Value = keyValuePair.Value & " has been modified"
> > Debug.WriteLine("The new value is " & keyValuePair.Value & ".")
> > Next
> >
> > The above code would iterate through only the 3 key/value pairs with a[/color][/color]
key[color=blue][color=green]
> > of "AAA" and modify their values. So...is all of this possible? Can
> > anyone
> > send me in the right direction? I'd really appreciate any help you can
> > give. Thanks!
> >
> >[/color]
>
>[/color] | | | | re: Implementing a key/value pair collection...with a catch
One additional note...I forgot that you can't change a collection when you
enumerate through it. Ignore that part of my original message where I
showed code modifying the items as I iterated through with For Each. Read
only access should be enough for what I need to do.
"Jeff L." <nospam@nospam.com> wrote in message
news:eeLfBcQ4DHA.416@TK2MSFTNGP10.phx.gbl...[color=blue]
> I think I get what you're saying...the problem is that if I do it that[/color]
way,[color=blue]
> the key/value pairs will no longer be correctly ordered. I need to keep
> them in their original order (which can be any order at all...no specific
> sorting or anything). If I stick them into a hashtable based on their key
> value, they won't be ordered correctly any more.
>
> Jeff
>
> "Ken Tucker [MVP]" <vb2ae@bellsouth.net> wrote in message
> news:Om$UOPQ4DHA.1052@TK2MSFTNGP12.phx.gbl...[color=green]
> > Hi,
> >
> > Here is a suggestion. You store any type of data in a[/color][/color]
hashtable.[color=blue][color=green]
> > Why not store an arraylist with all of your values for each key.
> >
> > Ken
> > -----------------------
> > "Jeff L." <nospam@nospam.com> wrote in message
> > news:%23NATeCQ4DHA.2480@TK2MSFTNGP09.phx.gbl...[color=darkred]
> > >I have an interesting problem and I'm not coming up with any answers in[/color][/color]
> my[color=green][color=darkred]
> > > searches, so hopefully someone can give me a hand with this. I have a
> > > feeling it's easy, but I usually get my nose stuck too far in the[/color][/color]
> details[color=green][color=darkred]
> > > to
> > > see the big picture. :)
> > >
> > > I want to create a collection of key/value pairs. I have a class for[/color][/color]
> the[color=green][color=darkred]
> > > key/value pair...we'll just call it KeyValuePair for this example.[/color][/color][/color]
Now,[color=blue][color=green][color=darkred]
> > > when creating the collection, I need to be able to have duplicate
> > > keys...this is why I can't just use a hashtable. One more requirement[/color][/color]
> is[color=green][color=darkred]
> > > that I can iterate through the entire collection, or just a subset of[/color][/color]
> the[color=green][color=darkred]
> > > collection based on a key. The final requirement is that all[/color][/color][/color]
key/value[color=blue][color=green][color=darkred]
> > > pairs stay in the order that I added them...I can't have them[/color][/color][/color]
rearranged[color=blue][color=green][color=darkred]
> > > when I iterate through them. So, let's say the following is the data
> > > stored
> > > in the collection:
> > >
> > > Key Value
> > > ------------
> > > AAA Test 1
> > > BBB Test 2
> > > AAA Test 3
> > > BBB Test 4
> > > AAA Test 5
> > > CCC Test 6
> > > DDD Test 7
> > > EEE Test 8
> > > EEE Test 9
> > >
> > > I'd like to be able to do this:
> > >
> > > For Each keyValuePair in keyValuePairCollection.Items
> > > Debug.WriteLine("The key is " & keyValuePair.Key & " and the value[/color][/color][/color]
is[color=blue]
> "[color=green][color=darkred]
> > > & keyValuePair.Value & ".")
> > > keyValuePair.Value = keyValuePair.Value & " has been modified"
> > > Debug.WriteLine("The new value is " & keyValuePair.Value & ".")
> > > Next
> > >
> > > The above code would iterate through all 9 key/value pairs and modify
> > > their
> > > values. I'd also like to be able to do this:
> > >
> > > For Each keyValuePair in KeyValuePairCollection.FilteredItems("AAA")
> > > Debug.WriteLine("The key is " & keyValuePair.Key & " and the value[/color][/color][/color]
is[color=blue]
> "[color=green][color=darkred]
> > > & keyValuePair.Value & ".")
> > > keyValuePair.Value = keyValuePair.Value & " has been modified"
> > > Debug.WriteLine("The new value is " & keyValuePair.Value & ".")
> > > Next
> > >
> > > The above code would iterate through only the 3 key/value pairs with a[/color][/color]
> key[color=green][color=darkred]
> > > of "AAA" and modify their values. So...is all of this possible? Can
> > > anyone
> > > send me in the right direction? I'd really appreciate any help you[/color][/color][/color]
can[color=blue][color=green][color=darkred]
> > > give. Thanks!
> > >
> > >[/color]
> >
> >[/color]
>
>[/color] |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,449 network members.
|