noob using dictionary as class property ?

G

GiJeet

hello, I'm trying to use a dictionary as a class member. I want to
use a property to get/set the key/value of the dictionary but I'm
confused as how to use a dictionary as a property. Since there are 2
parts, I don't know how to setup the get/sets. I tried searching but
could not find any examples. I'd appreciate an example of how to get/
set the two parts of a dictionary via a property of a class.

tia
 
J

Jeremy Shovan

using System.Collections.Generic;

namespace ClassLibrary
{
public class SampleClass
{
private IDictionary<string, object> myDictionary = new Dictionary<string,
object>();

public IDictionary<string, object> MyProperty
{
get { return myDictionary; }
set { myDictionary = value; }
}
}

public class Class2
{
public void DoSomething()
{
SampleClass sc = new SampleClass();
sc.MyProperty["key"] = new object();
sc.MyProperty["key2"] = new object();
}
}
}

Jeremy Shovan
 
P

Peter Bromberg [C# MVP]

it sounds like you really may want to make the dictionary private to the
class and not expose the actual dictionary itself as a property - instead
exposing getter / setter semantics that pass - in the setter, the key and
value, and in the getter, the key.
Inside the body of your setter you would perform the Add method on your
dictionary, and in the body of your setter you would return the value from
the dictionary indexer by the passed in key. Of course the parameters for
the key and or key+value would have to match your dictionary types.
Peter
 
G

GiJeet

it sounds like you really may want to make the dictionary private to the
I thought about using getter/setter methods but Jeremy's solution
worked.
I didn't know you could use this type of syntax: sc.MyProperty["key"]
= new object();
It even works with the Add method. sc.MyProperty.Add("FirstName",
"Gi");

I know there is a slight difference but don't remember what it
is... :)

This line has me puzzled thou:
private IDictionary<string, object> myDictionary = new
Dictionary<string,object>();

why IDictionary? we have both a get and set so we're not concerned
about mutation, so why not just Dictionary?

G
 
J

Jeremy Shovan

--- > private IDictionary<string, object> myDictionary = new
Dictionary<string,object>(); <---

This line is just something that I do when I write code. I always use the
most base class or interface that I need so that things can be changed out
without much work later. For example, at some later time I may create my own
special implementation of a Dictionary that implements the IDictionary
class. If I do this I would want code that I have written in the past to
still work with my custom implementation. If I have something that only
excepted a Dictionary instead of IDictionary then I would only be able to
use the Dictionary class and no other type of implementation. Take the
following for example..

public class SampleClass
{
private IDictionary<string, object> myDictionary;

public IDictionary<string, object> MyProperty
{
get { return myDictionary; }
set { myDictionary = value; }
}

public void DoSomethingWithTheDictionary()
{
// Do something here.
}
}

public class Class2
{
public void DoSomething()
{
SampleClass sc = new SampleClass();
sc.MyProperty = new Dictionary<string, object>();
sc.MyProperty.Add("something", new object());
sc.DoSomethingWithTheDictionary();

// The above will work just great if you used the Dictionary class
// but what if you want to do the operation on own dictionary
implementation?
// You would have to use the IDictionary interface instead. This will
allow you
// to do something like the following.

sc.MyProperty = new MyCustomDictionary();
sc.MyProperty.Add("somethingElse", new object());
sc.DoSomethingWithTheDictionary();
}
}

public class MyCustomDictionary : IDictionary<string, object>
{
public bool ContainsKey(string key)
{
throw new System.NotImplementedException();
}
public void Add(string key, object value)
{
throw new System.NotImplementedException();
}
public bool Remove(string key)
{
throw new System.NotImplementedException();
}
public bool TryGetValue(string key, out object value)
{
throw new System.NotImplementedException();
}
public object this[string key]
{
get { throw new System.NotImplementedException(); }
set { throw new System.NotImplementedException(); }
}
public ICollection<string> Keys
{
get { throw new System.NotImplementedException(); }
}
public ICollection<object> Values
{
get { throw new System.NotImplementedException(); }
}
public void Add(KeyValuePair<string, object> item)
{
throw new System.NotImplementedException();
}
public void Clear()
{
throw new System.NotImplementedException();
}
public bool Contains(KeyValuePair<string, object> item)
{
throw new System.NotImplementedException();
}
public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
{
throw new System.NotImplementedException();
}
public bool Remove(KeyValuePair<string, object> item)
{
throw new System.NotImplementedException();
}
public int Count
{
get { throw new System.NotImplementedException(); }
}
public bool IsReadOnly
{
get { throw new System.NotImplementedException(); }
}
IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string,
object>>.GetEnumerator()
{
throw new System.NotImplementedException();
}
public IEnumerator GetEnumerator()
{
return ((IEnumerable<KeyValuePair<string, object>>)this).GetEnumerator();
}
}


GiJeet said:
I thought about using getter/setter methods but Jeremy's solution
worked.
I didn't know you could use this type of syntax: sc.MyProperty["key"]
= new object();
It even works with the Add method. sc.MyProperty.Add("FirstName",
"Gi");

I know there is a slight difference but don't remember what it
is... :)

This line has me puzzled thou:
private IDictionary<string, object> myDictionary = new
Dictionary<string,object>();

why IDictionary? we have both a get and set so we're not concerned
about mutation, so why not just Dictionary?

G
 
G

GiJeet

This line is just something that I do when I write code. I always use the
most base class or interface that I need so that things can be changed out
without much work later. For example, at some later time I may create my own
special implementation of a Dictionary that implements the IDictionary
class. If I do this I would want code that I have written in the past to
still work with my custom implementation. If I have something that only
excepted a Dictionary instead of IDictionary then I would only be able to
use the Dictionary class and no other type of implementation. Take the
following for example..

I see. Excellent. Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top