473,320 Members | 1,535 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.

Generic IDictionary confusion

trying to design an effiecient interface, where I can pass an
IDictionary<int,string^to a method. int is some id and string^ is
some
value that is initialized to null by a caller.

The callee function needs to update the value based on the key.

in native code I'd have passed a std::map and do ((*it).second) = new
std::string("test");

I don't see a way to do it in managed code.

I obviously can't assign it to keyValuePair element as it's read only.
And I can't even do this:

for each ( KeyValuePair<int, string^>^ iter in testCollection )
{
testCollection[iter->key] = "test"
}

as on the second pass of the loop, the code above will throw since the
colleciton is modified.

Thus, it looks like I need 2 passes, where I'd make a list of ids in
the first pass and on the second pass, I'd do something
like:
for ( int i = 0; i < ListIds.Count; ++i )
{
testCollection[listIds[i]] = "test";
}

This code seems very poor. Is there really no way to update the
IDictionary<key,valuein 1 pass? I've got to be missing some
IEnumerator that would let me do that :)

Thanks in advance!
vcq

Sep 14 '07 #1
7 1519
On Sep 13, 10:45 pm, vcquestions <vcquesti...@gmail.comwrote:
trying to design an effiecient interface, where I can pass an
IDictionary<int,string^to a method. int is some id and string^ is
some
value that is initialized to null by a caller.

The callee function needs to update the value based on the key.

in native code I'd have passed a std::map and do ((*it).second) = new
std::string("test");

I don't see a way to do it in managed code.

I obviously can't assign it to keyValuePair element as it's read only.
And I can't even do this:

for each ( KeyValuePair<int, string^>^ iter in testCollection )
{
testCollection[iter->key] = "test"

}

as on the second pass of the loop, the code above will throw since the
colleciton is modified.

Thus, it looks like I need 2 passes, where I'd make a list of ids in
the first pass and on the second pass, I'd do something
like:
for ( int i = 0; i < ListIds.Count; ++i )
{
testCollection[listIds[i]] = "test";

}

This code seems very poor. Is there really no way to update the
IDictionary<key,valuein 1 pass? I've got to be missing some
IEnumerator that would let me do that :)

Thanks in advance!
vcq
any suggestions?

Sep 14 '07 #2

"vcquestions" <vc*********@gmail.comwrote in message
news:11*********************@57g2000hsv.googlegrou ps.com...
trying to design an effiecient interface, where I can pass an
IDictionary<int,string^to a method. int is some id and string^ is
some
value that is initialized to null by a caller.

The callee function needs to update the value based on the key.

in native code I'd have passed a std::map and do ((*it).second) = new
std::string("test");

I don't see a way to do it in managed code.

I obviously can't assign it to keyValuePair element as it's read only.
And I can't even do this:

for each ( KeyValuePair<int, string^>^ iter in testCollection )
{
testCollection[iter->key] = "test"
}

as on the second pass of the loop, the code above will throw since the
colleciton is modified.
You could file a bug against this, as it's improper behavior. Iterators
should only be invalidated when the morphology (shape) of the collection
changes, and updating a value doesn't do that. The version number
increment, which is causing that exception, should only be done when an item
is added or removed.

BTW KeyValuePair is a value type, so you shouldn't use ^ with it (doing so
isn't outright wrong, but it does slow you down because of boxing and
unboxing).
>
Thus, it looks like I need 2 passes, where I'd make a list of ids in
the first pass and on the second pass, I'd do something
like:
for ( int i = 0; i < ListIds.Count; ++i )
{
testCollection[listIds[i]] = "test";
}

This code seems very poor. Is there really no way to update the
IDictionary<key,valuein 1 pass? I've got to be missing some
IEnumerator that would let me do that :)

Thanks in advance!
vcq
Sep 14 '07 #3
On Sep 14, 12:56 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
"vcquestions" <vcquesti...@gmail.comwrote in message

news:11*********************@57g2000hsv.googlegrou ps.com...


trying to design an effiecient interface, where I can pass an
IDictionary<int,string^to a method. int is some id and string^ is
some
value that is initialized to null by a caller.
The callee function needs to update the value based on the key.
in native code I'd have passed a std::map and do ((*it).second) = new
std::string("test");
I don't see a way to do it in managed code.
I obviously can't assign it to keyValuePair element as it's read only.
And I can't even do this:
for each ( KeyValuePair<int, string^>^ iter in testCollection )
{
testCollection[iter->key] = "test"
}
as on the second pass of the loop, the code above will throw since the
colleciton is modified.

You could file a bug against this, as it's improper behavior. Iterators
should only be invalidated when the morphology (shape) of the collection
changes, and updating a value doesn't do that. The version number
increment, which is causing that exception, should only be done when an item
is added or removed.

BTW KeyValuePair is a value type, so you shouldn't use ^ with it (doing so
isn't outright wrong, but it does slow you down because of boxing and
unboxing).


Thus, it looks like I need 2 passes, where I'd make a list of ids in
the first pass and on the second pass, I'd do something
like:
for ( int i = 0; i < ListIds.Count; ++i )
{
testCollection[listIds[i]] = "test";
}
This code seems very poor. Is there really no way to update the
IDictionary<key,valuein 1 pass? I've got to be missing some
IEnumerator that would let me do that :)
Thanks in advance!
vcq- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -
Thanks Ben! On the iteration ( 1-st question ) do you see a clean
workaround ( a different method of iterating through collection &
updating values )?
Also, thanks for pointing out the KeyValuePair issue!

Sep 14 '07 #4
>>
Thanks in advance!
vcq- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

Thanks Ben! On the iteration ( 1-st question ) do you see a clean
workaround ( a different method of iterating through collection &
updating values )?
Also, thanks for pointing out the KeyValuePair issue!
As I said, I think it is a bug in the BCL Dictionary<TKey,TValue>::Insert
helper function. There are two code paths, one for when the key already
exists, and one where it does not. The first of those should not increment
the private version variable which effectively kills all existing
enumerators.

If you submit a bug on Connect, post the link here. I will validate it and
vote for it.

Sep 14 '07 #5
On Sep 14, 1:21 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
Thanks in advance!
vcq- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
Thanks Ben! On the iteration ( 1-st question ) do you see a clean
workaround ( a different method of iterating through collection &
updating values )?
Also, thanks for pointing out the KeyValuePair issue!

As I said, I think it is a bug in the BCL Dictionary<TKey,TValue>::Insert
helper function. There are two code paths, one for when the key already
exists, and one where it does not. The first of those should not increment
the private version variable which effectively kills all existing
enumerators.

If you submit a bug on Connect, post the link here. I will validate it and
vote for it.
I got the bug part - I just refused to believe that MSFT gives us such
pleasant surprises -:) ( but after Debug::Assert( 0 ) showing up in
release mode - oh well... )

here's the link:
https://connect.microsoft.com/Visual...dbackID=298166

Sep 14 '07 #6

"vcquestions" <vc*********@gmail.comwrote in message
news:11**********************@57g2000hsv.googlegro ups.com...
On Sep 14, 1:21 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
Thanks in advance!
vcq- Hide quoted text -
>- Show quoted text -- Hide quoted text -
>- Show quoted text -
Thanks Ben! On the iteration ( 1-st question ) do you see a clean
workaround ( a different method of iterating through collection &
updating values )?
Also, thanks for pointing out the KeyValuePair issue!

As I said, I think it is a bug in the BCL Dictionary<TKey,TValue>::Insert
helper function. There are two code paths, one for when the key already
exists, and one where it does not. The first of those should not
increment
the private version variable which effectively kills all existing
enumerators.

If you submit a bug on Connect, post the link here. I will validate it
and
vote for it.

I got the bug part - I just refused to believe that MSFT gives us such
pleasant surprises -:) ( but after Debug::Assert( 0 ) showing up in
release mode - oh well... )

here's the link:
https://connect.microsoft.com/Visual...dbackID=298166
They appear to be hiding behind "It's not wrong, because we choose to define
the correct behavior as the way it works". I've added a bunch of counter
examples -- in C++/CLI -- to the bug report in the hope that somebody
realizes that there's value in having .NET collections act the same way
every other collection in the world does.

Sep 28 '07 #7
Thanks Ben! Frankly, I was not even familiar with Connect before this
issue come up. I always open bugs with MSFT by calling Dev. support
and shelling out $250 ( then they refund it eventually ). Obviously,
this enthusiasm could go only so far...:) We'll see if this could get
escalated.
On Sep 28, 11:23 am, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
"vcquestions" <vcquesti...@gmail.comwrote in message

news:11**********************@57g2000hsv.googlegro ups.com...


On Sep 14, 1:21 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
Thanks in advance!
vcq- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
Thanks Ben! On the iteration ( 1-st question ) do you see a clean
workaround ( a different method of iterating through collection &
updating values )?
Also, thanks for pointing out the KeyValuePair issue!
As I said, I think it is a bug in the BCL Dictionary<TKey,TValue>::Insert
helper function. There are two code paths, one for when the key already
exists, and one where it does not. The first of those should not
increment
the private version variable which effectively kills all existing
enumerators.
If you submit a bug on Connect, post the link here. I will validate it
and
vote for it.
I got the bug part - I just refused to believe that MSFT gives us such
pleasant surprises -:) ( but after Debug::Assert( 0 ) showing up in
release mode - oh well... )
here's the link:
https://connect.microsoft.com/Visual...wFeedback.aspx...

They appear to be hiding behind "It's not wrong, because we choose to define
the correct behavior as the way it works". I've added a bunch of counter
examples -- in C++/CLI -- to the bug report in the hope that somebody
realizes that there's value in having .NET collections act the same way
every other collection in the world does.- Hide quoted text -

- Show quoted text -

Sep 29 '07 #8

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

Similar topics

4
by: John C | last post by:
I'm new to C#, so just point me at the correct reference material if this question has been answered before. When creating a new class which implements the IDictionary interface, two versions of...
5
by: cody | last post by:
I know I can use ArrayList.ReadOnly() to create a readonly version of it, but how can I achive a similar thing with a generic collection of .NET 2.0?
8
by: Steven Cummings | last post by:
Hello, I've scoured this usenet group and didn't find anything specific to my problem, so hopefully this won't be a repeated question. I'm all but certain it's not. I would like to *declare*...
4
by: Adam Clauss | last post by:
I ran into a problem a while back when attempting to convert existing .NET 1.1 based code to .NET 2.0 using Generic collections rather than Hashtable, ArrayList, etc. I ran into an issue because...
18
by: Rune B | last post by:
Hi Group I was considering using a Generic Dictionary<> as a value container inside my business objects, for the reason of keeping track of fields changed or added and so on. - But how...
3
by: Marek | last post by:
Hi I have a class which has a property which implements IDictionary which I would like to display in a propertygrid control. If I add the property expands, but I only get Count, IsReadOnly,...
0
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...
2
by: =?Utf-8?B?RGF2aWQgTW9ycmlz?= | last post by:
I am trying to create a nested Dictionary and get an error that seems odd to me. Here is my declaration: private IDictionary<Guid, IDictionary<Guid, string>> myNestedDictionary = new...
1
by: pierre.k | last post by:
Hello, how can I do a cast like IDictionary<int, stringas IDictionary<object, string ? Thanks in advance for your help, pierre.k
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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: 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...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.