Dictionary of dictionaries

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I’m looking for a way to create a generic dictionary of generic dictionaries
grouped by type. Something like this

Class the Grouping ChildItem<T>{…}

Class ParentGrouping {
Dictionary<Type, ChildItem <theGrouppingItemtype>> myDimDict;
}

So I would access it like this

ParentGrouping[System.string, “myitemKeyâ€] = “new valueâ€;
OR
ParentGrouping[System.Drawing.Point, “myitemKeyâ€] = new
System.Drawing.Point(0,0);



Any ideas?
 
So I would access it like this
ParentGrouping[System.string, "myitemKey"] = "new value";
OR
ParentGrouping[System.Drawing.Point, "myitemKey"] = new
System.Drawing.Point(0,0);


You can do something like this.

Dictionary<string, MyClass> myclass = new Dictionary<string,
MyClass>();
myclass.Add("one", new MyClass(new System.Drawing.Point(1, 2)));
myclass.Add("two", new MyClass("Hello"));


The complete example (a console app):

using System;
using System.Collections.Generic;
using System.Text;

namespace Generics_Dictionary
{

class MyClass
{
private System.Drawing.Point _point = new System.Drawing.Point();
private string _MyText = "";
private bool EmptyText = true;

public MyClass(System.Drawing.Point point)
{
_point = point;
}

public MyClass(string MyText)
{
this.EmptyText = false;
this._MyText = MyText;
}

public string MyText
{
get
{
return this._MyText;
}
set
{
this.EmptyText = false;
this._MyText = value;
}
}

public System.Drawing.Point MyPoint
{
get
{
return this._point;
}
set
{
this._point = value;
}
}

public bool IsTextEmpty
{
get
{
return this.EmptyText;
}
}
}


class Program
{
public Program()
{
Dictionary<string, MyClass> myclass = new Dictionary<string,
MyClass>();
myclass.Add("one", new MyClass(new System.Drawing.Point(1, 2)));
myclass.Add("two", new MyClass("Hello"));

foreach (KeyValuePair<string, MyClass> cls in myclass)
{

if (!cls.Value.MyPoint.IsEmpty)
System.Console.WriteLine("Text : " + cls.Value.MyPoint.X
+ "." + cls.Value.MyPoint.Y);
if (!cls.Value.IsTextEmpty)
System.Console.WriteLine("Text : " + cls.Value.MyText);

}
System.Console.ReadKey();
}

static void Main(string[] args)
{
new Program();
}
}
}

Regards,
Lars-Inge Tønnessen
 
First, thanks for taking the time to respond.
The crux of my problem resides in the fact that your version of MyClass is
not a strongly typed class.
MyClass<T> would be what I’m looking for. Reason is that the types to be
stored in each list is not limited to string and point.
That’s why I wanted to tie the T for MyClass<T> to the key of types in the
parent Dictionary.
 

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

Back
Top