0

I have following parent class:

public class BaseType
{ 
   public abstract Dictionary<string, object> dict { get; set; }
}

Child class:

public override Dictionary<string, object> dict
{
   get 
   {
      return fn(); 
   }
   set
   {
      //set dictionary[key]=value }
   }

fn is implemented in child class as:

public static Dictionary<string, object> fn()
{
   Dictionary<string, object> dictionary = new Dictionary<string, object>();
   dictionary.Add("key1", "0");
   dictionary.Add("key2", "something");
   return dictionary;
}

I need to access this dictionary as follows:

BaseType test=new Child();
test.dict["key1"]=1;//set property
object strGet= test.dict["key2];//get property

How can I achieve the above get and set?

pika
  • 11
  • 1
  • 8
  • please write valid c# code. in your base class is that property? or method? and why abstract? – M.kazem Akhgary Nov 20 '15 at 11:00
  • 1
    `test.dict["key1"]` still uses the `get` part of the property `dict`. you don't seem to use `set` at all. However, with `get { return fn(); }` you always return a *new* instance! So with `test.dict["key1"] = 1; object obj = test.dict["key1"]`, `obj` will be `"0"`, because the dictionary where you set the value of `"key1"` to `1` is lost. – Corak Nov 20 '15 at 11:03
  • @pika as you deleted your answer I'm posting my comment here again: I would still suggest though that you clear up your question as it seemed from your answer that you meant things differently from how they came over in your question. – Thomas Nov 24 '15 at 08:13

2 Answers2

3

Your parent class is already invalid. You cannot have a property that takes arguments.

You can only expose the dictionary as a property itself. Note that you also need to make the class abstract as well:

public abstract class BaseType
{
    public abstract Dictionary<string, object> Dict { get; set; }
}

Then, in subtypes of that class, you can set up the getter and setter so it returns a custom dictionary:

public class MyType : BaseType
{
    public override Dictionary<string, object> Dict
    {
        get
        {
            return GetSomeDictionary();
        }
        set
        {
            DoSomethingWith(value);
        }
    }
}

Note, that this does not allow you to overwrite the behavior when you do someObj.Dict["key"] = "foo". The item accessor is built into the dictionary type, and you cannot overwrite that from within your class.

What you could do is expose a IDictionary<string, object> instead and provide your own type that wraps a normal dictionary but exposes your desired behavior instead.


If the whole purpose of your code is just to provide some default value for the dictionary, then you can solve this a lot easier:

public class MyType : BaseType
{
    private Dictionary<string, object> _dict = null;

    public override Dictionary<string, object> Dict
    {
        get
        {
            if (_dict == null)
            {
                _dict = InitializeDictionary();
            }
            return _dict;
        }
        set
        {
            _dict = value;
        }
    }
}

Where InitializeDictionary() returns a new dictionary with the default values.

poke
  • 369,085
  • 72
  • 557
  • 602
  • How can I provide 'value' dynamically? – pika Nov 20 '15 at 11:24
  • 1
    With [set](https://msdn.microsoft.com/library/w86s7x04.aspx), you always have a `value` which represents the value you want to set the property to. -- basically `set` is syntactic sugar for `public [propertyname]_set([propertytype] value){ /* body of set */ }` – Corak Nov 20 '15 at 12:14
-3

I got it!! This way we can dynamically set the value of any key in dictionary.

public object objValue;
public string strKey;
public override Dictionary<string, object> dictionary
    {
        get
        {
           return fn(); 
        }
        set 
        {
            setTest(strKey,objValue);
        }
    }

    public void setTest(string strKey, object objValue)
    {
        dictionary[strKey] = objValue;
    }
pika
  • 11
  • 1
  • 8
  • 3
    In `dictionary.set`, what is `strKey` and what is `objValue` (in other words, where are they defined and what values are they)? You also ignore the `value` given to `set`. I don't see from what you have here how this answers your own question. – crashmstr Nov 20 '15 at 13:02
  • strKey and objValue seem to be other variables from within the same class but what is strange is.........value is completely and utterly ignored which is how set is given a parameter. Thus the "set" has not a normal functionality. Instead it sets an array or dictionary that is called param and never mentioned before......,not sure what this is about but either the question or the answer is faulty or quite possibly both are quite faulty oO – Thomas Nov 24 '15 at 06:57
  • @pika I put a -1 until you clear things up there either in the question or the answer or both so that things make sense – Thomas Nov 24 '15 at 06:58
  • @Thomas, I don't know what seems wrong to you but this is working for me. I'll answer you but please make your questions clear – pika Nov 24 '15 at 10:31
  • 1
    @pika as I mentnioned you are; 1. setting a variable param which is never mentioned anywhere (not in your question, not in this answer). which makes this answer incomplete to anyone who reads it. – Thomas Nov 24 '15 at 10:38
  • 1
    2. You are using the normal set property BUT you are ignoring the value parameter it automatically has which is strange and you didn't give any reason for this. For example http://stackoverflow.com/questions/11583595/how-to-write-a-getter-and-setter-for-a-dictionary the value parameter has also key and value for dictionaries: Set(value.Key, value.Value); thus your usage of objValue, strKey where you also don't show how they are set or at least give any comment why they are used indicates incompleteness as another reader can't fathom WHY you use those instead of value. – Thomas Nov 24 '15 at 10:38
  • thus in essence the problem with your answer (and reason for the downvotes....not only mine) is that it is confusing to people why things are done there and why/how it solves the question (sence the "incomplete" feeling it gives as stated under 1 and 2) – Thomas Nov 24 '15 at 10:39
  • @Thomas, its working the same way as mentioned here http://stackoverflow.com/questions/11583595/how-to-write-a-getter-and-setter-for-a-dictionary. This code snippet worked for me, hence didn't find a reason to use value.Key and value.Value. You should try setter with this snippet. – pika Nov 24 '15 at 10:53
  • @pika the difference to yours is that THERE they mention only variables,... that are indicated to exist and what they stand for in both the question and/or the answer. Additionally if something is not clear from code it is mentioned textwise in the answer. which is what is plainly missing for your "solution". The answers should always be in such a way so that a third person can have a chance to understand it (same for the question btw). and with missing infos about why or missing descriptions about variables used......that is clearly not the case. which led to downvotes. – Thomas Nov 24 '15 at 11:18
  • @pika I dont see any difference (maybe because the answer is deleted?) – Thomas Nov 24 '15 at 12:03
  • @Thomas, the answer is still here. not deleted! – pika Nov 24 '15 at 12:07
  • @pika ah the gray out is because of the points the answers has (-3+ means gray out). just looked it up thought before it was a delete. I removed my downvote as my key problem with your answer is solved. Although it would be good if you also add a reason why you use objvalue and keyvalue instead of the value. variants (dont think it will change the current points but for future questions its always good to keep it in mind how / why most ppl downvote and what to best do against that.) – Thomas Nov 24 '15 at 12:14