473,236 Members | 1,771 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,236 software developers and data experts.

noob using dictionary as class property ?

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

Jun 27 '08 #1
6 10122
using System.Collections.Generic;

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

public IDictionary<string, objectMyProperty
{
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
"GiJeet" <gi****@yahoo.comwrote in message
news:5f**********************************@a1g2000h sb.googlegroups.com...
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
Jun 27 '08 #2
On Thu, 15 May 2008 10:03:57 -0700, GiJeet <gi****@yahoo.comwrote:
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.
What do you mean by "2 parts"? An instance of the Dictionary<TKey,
TValueclass is still just one instance. References to such an instance
are dealt with just as references to any other class would be. If you
already know how to use properties and types, you already know how to make
a property that's a Dictionary<TKey, TValue>.

Pete
Jun 27 '08 #3
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

"GiJeet" <gi****@yahoo.comwrote in message
news:5f**********************************@a1g2000h sb.googlegroups.com...
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
Jun 27 '08 #4
>>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.
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, objectmyDictionary = 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
Jun 27 '08 #5
--- private IDictionary<string, objectmyDictionary = 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, objectmyDictionary;

public IDictionary<string, objectMyProperty
{
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<stringKeys
{
get { throw new System.NotImplementedException(); }
}
public ICollection<objectValues
{
get { throw new System.NotImplementedException(); }
}
public void Add(KeyValuePair<string, objectitem)
{
throw new System.NotImplementedException();
}
public void Clear()
{
throw new System.NotImplementedException();
}
public bool Contains(KeyValuePair<string, objectitem)
{
throw new System.NotImplementedException();
}
public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
{
throw new System.NotImplementedException();
}
public bool Remove(KeyValuePair<string, objectitem)
{
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" <gi****@yahoo.comwrote in message
news:7b**********************************@a70g2000 hsh.googlegroups.com...
>>>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.

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, objectmyDictionary = 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
Jun 27 '08 #6
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!

Jun 27 '08 #7

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

Similar topics

1
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex...
1
by: Martin Widmer | last post by:
Hi Folks. When I iterate through my custom designed collection, I always get the error: "Unable to cast object of type 'System.Collections.DictionaryEntry' to type...
6
by: Julien | last post by:
Hello, I have some files located in a file server and managed by a SQL database from a web based interface using ASP + VBSCRIPT technology. I need to automatically copy those files to a web...
1
by: Nick | last post by:
I am working on a website for a client and one of their requirements was to have a mailing list. I decided to XSLT to transform "templates" to HTML so that editing was very easy and less time...
4
by: NullQwerty | last post by:
Hi folks, I have a Dictionary which contains a string key and an object value. I want the object value to point to a property in my class and I want it to be by reference, so that later on I...
20
by: Gustaf | last post by:
This is two questions in one really. First, I wonder how to convert the values in a Dictionary to an array. Here's the dictionary: private Dictionary<Uri, Schemaschemas = new Dictionary<Uri,...
8
by: Papa.Coen | last post by:
After repeatedly calling/using a Dictionary I get a StackOverflowError. What am I doing wrong? The situation is as follows: I have ruler class, this class contains some member variables,...
2
by: Carnell, James E | last post by:
I am thinking about purchasing a book, but wanted to make sure I could get through the code that implements what the book is about (Artificial Intelligence a Modern Approach). Anyway, I'm not a...
1
by: GVDC | last post by:
Example server-side JavaScript Web script, Dictionary class //Dictionary class, hash array unlimited length configurable speed/efficiency // printf("<html><body>"); printf("<b>Creating...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
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...

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.