Overriding a Dictionary's Item property
-
Hello TCP gurus! It's me again, standing in a the corner of a room with wet paint all around me.... Any help would be appreciated. I have a dictionary
public class DataImage : Dictionary<string,>
which has been working wonderfully for me. But now I find that when I have a problem when adding values using the Item property.
DataImage<string,> modImage = new DataImage<string,>();
...
modImage["xxx"] = "yyy";I need to have some additional actions occur. In particular, DataImage contains some private data that needs to be updated whenever an element is added or removed. I have the required code in the Add() and Remove() methods, but I also need that logic to occur when the dictionary is accessed using its Item property. I would like to do something akin to
public override string Item[key]
{
get { return base[key]; }
set
{
base[key] = value;
changeOtherStuff(key, value);
}
}I looked at this page (http://msdn.microsoft.com/en-us/library/9tee9ht2.aspx[^]), but couldn't see how to write the necessary code from it. I also searched online, but to no avail.
Clive Pottinger Victoria, BC
-
Hello TCP gurus! It's me again, standing in a the corner of a room with wet paint all around me.... Any help would be appreciated. I have a dictionary
public class DataImage : Dictionary<string,>
which has been working wonderfully for me. But now I find that when I have a problem when adding values using the Item property.
DataImage<string,> modImage = new DataImage<string,>();
...
modImage["xxx"] = "yyy";I need to have some additional actions occur. In particular, DataImage contains some private data that needs to be updated whenever an element is added or removed. I have the required code in the Add() and Remove() methods, but I also need that logic to occur when the dictionary is accessed using its Item property. I would like to do something akin to
public override string Item[key]
{
get { return base[key]; }
set
{
base[key] = value;
changeOtherStuff(key, value);
}
}I looked at this page (http://msdn.microsoft.com/en-us/library/9tee9ht2.aspx[^]), but couldn't see how to write the necessary code from it. I also searched online, but to no avail.
Clive Pottinger Victoria, BC
-
Hello TCP gurus! It's me again, standing in a the corner of a room with wet paint all around me.... Any help would be appreciated. I have a dictionary
public class DataImage : Dictionary<string,>
which has been working wonderfully for me. But now I find that when I have a problem when adding values using the Item property.
DataImage<string,> modImage = new DataImage<string,>();
...
modImage["xxx"] = "yyy";I need to have some additional actions occur. In particular, DataImage contains some private data that needs to be updated whenever an element is added or removed. I have the required code in the Add() and Remove() methods, but I also need that logic to occur when the dictionary is accessed using its Item property. I would like to do something akin to
public override string Item[key]
{
get { return base[key]; }
set
{
base[key] = value;
changeOtherStuff(key, value);
}
}I looked at this page (http://msdn.microsoft.com/en-us/library/9tee9ht2.aspx[^]), but couldn't see how to write the necessary code from it. I also searched online, but to no avail.
Clive Pottinger Victoria, BC
Few observations: - when defining Item property (indexer), I believe you should provide type of the key parameter - indexer is defined using this keyword - default indexer is not marked virtual so you should use new keyword So the code should be something like the following
public class DataImage : Dictionary<string,sometype> {
...
public new sometype this[string key} {
get {...Hope this helps, Mika
The need to optimize rises from a bad design. My articles[^]
-
Hello TCP gurus! It's me again, standing in a the corner of a room with wet paint all around me.... Any help would be appreciated. I have a dictionary
public class DataImage : Dictionary<string,>
which has been working wonderfully for me. But now I find that when I have a problem when adding values using the Item property.
DataImage<string,> modImage = new DataImage<string,>();
...
modImage["xxx"] = "yyy";I need to have some additional actions occur. In particular, DataImage contains some private data that needs to be updated whenever an element is added or removed. I have the required code in the Add() and Remove() methods, but I also need that logic to occur when the dictionary is accessed using its Item property. I would like to do something akin to
public override string Item[key]
{
get { return base[key]; }
set
{
base[key] = value;
changeOtherStuff(key, value);
}
}I looked at this page (http://msdn.microsoft.com/en-us/library/9tee9ht2.aspx[^]), but couldn't see how to write the necessary code from it. I also searched online, but to no avail.
Clive Pottinger Victoria, BC
sorry. The definition of DataImage should say "string,string", but the display is dropping the second "string". And for some reason, I can't get in to edit to the message.
Clive Pottinger Victoria, BC
-
cpotting wrote:
I also searched online, but to no avail.
They hide that information in the documentation[^]
led mike
Thank you Mike - exactly the syntax I needed. Now I can hang from ceiling and continue painting the floor...
Clive Pottinger Victoria, BC
-
Few observations: - when defining Item property (indexer), I believe you should provide type of the key parameter - indexer is defined using this keyword - default indexer is not marked virtual so you should use new keyword So the code should be something like the following
public class DataImage : Dictionary<string,sometype> {
...
public new sometype this[string key} {
get {...Hope this helps, Mika
The need to optimize rises from a bad design. My articles[^]
Thank you too, Mika
Clive Pottinger Victoria, BC