Click here to Skip to main content
15,905,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here is what I would like to do:

Dictionary<string,Dictionary<string, XxXxXx>> BoB;


where XxXxXx can be class A, class B, class C...etc
and {A,B,C}:Class Z

so that i can declare (roughly-Pseudo)

class Rusty
{

     private Dictionary<String,A> _a;
     private Dictionary<String,B> _b;
     private Dictionary<String,C> _c;

     public Dictionary<String,????> Lookup( identifier id)
     {
          switch(id)
          {
               case id.a: return _a;
               case id.b: return _b;
               case id.c: return _c;}
          }
     }
}


yet the closest I can get results in:
CS1502, CS0029, CS0039 X|

Cannot convert type 'System.Collections.Generic.Dictionary<string,ClassA>' to 'System.Collections.Generic.Dictionary<string,ClassZ>' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

..basically says that what i have above can't be done..

couldn't find a decent reference for this type of "issue":confused:

any ideas?


As per aspdevdotnet's suggestion(thank you)

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

namespace Jumba
{
    class A:Z
    {
    }
    class B:Z
    {
    }
    class C:Z
    {
    }
    class Z
    {
    }
    class Y 
    {
        Dictionary<string, Dictionary<string,Z>> Bob = new Dictionary<string,Dictionary<string,Z>>();
        Dictionary<string, A> _a = new Dictionary<string,A>();
        Dictionary<string, B> _b = new Dictionary<string,B>();
        Dictionary<string, C> _c = new Dictionary<string,C>();
        public Y() 
        {
            //Error	4	Argument '2': cannot convert from 'System.Collections.Generic.Dictionary<string,Jumba.A>' to 'System.Collections.Generic.Dictionary<string,Jumba.Z>'	
            Bob.Add("a",_a);
            //Error	6	Argument '2': cannot convert from 'System.Collections.Generic.Dictionary<string,Jumba.A>' to 'System.Collections.Generic.Dictionary<string,Jumba.Z>'	
            Bob.Add("b",_b);
            //Error	8	Argument '2': cannot convert from 'System.Collections.Generic.Dictionary<string,Jumba.A>' to 'System.Collections.Generic.Dictionary<string,Jumba.Z>'	
            Bob.Add("c",_c);
        }
        public Dictionary<string, Z> Retrieve(string str) 
        {
            Object alpha = Bob[str];
            //Error	15	Cannot implicitly convert type 'Jumba.A' to 'System.Collections.Generic.Dictionary<string,Jumba.Z>'	
            if (alpha is A){return (alpha as A)}
        
        }
    }

}
Posted
Updated 6-Dec-09 12:10pm
v3

Regarding your updated sample. You seem to have missed the point. This is what you want:
C#
//  Your dictionaries will be of value Z (so, A, B, and C instances can be placed in it).
Dictionary<string, Z> _a = new Dictionary<string,Z>();
Dictionary<string, Z> _b = new Dictionary<string,Z>();
Dictionary<string, Z> _c = new Dictionary<string,Z>();

// Your retrieve method will return a dictionary, not a Z derived instance.
public Dictionary<string, Z> Retrieve(string str)
{
    return Bob[str];
}

Or, you could have 3 Retrieve methods if you want to do the work for the user of this class:
C#
public A RetrieveA(string str) { return Bob["a"][str] as A; }
public B RetrieveB(string str) { return Bob["b"][str] as B; }
public C RetrieveC(string str) { return Bob["c"][str] as C; }
 
Share this answer
 
Since C# is strongly typed, your types are clashing. What you can do is have classes A, B, and C derive from som common parent class (say, AlphaClass), then create a dictionary of that type. Or, since all classes derive from the Object class, you could just use Object instead of AlphaClass:
C#
// A, B, and C should derive from AlphaClass.
Dictionary<string, Dictionary<string, AlphaClass>> BoB;
// Or, just use Object, since they already derive from that class.
Dictionary<string, Dictionary<string, Object>> BoB;

When you get one of the items from the dictionary, you can then just check its type and convert it to that type:
C#
Object alpha = BoB["A"]["someKey"];
if(alpha is A) (alpha as A).SomeAMethod();
 
Share this answer
 
This exactly what the interfaces do in .Net
Make an interface represent the common stuff between all the classes Ex
public interface IInterface
       {
           int number{get; set;}
           void doSomething();
       }


Then implement this interface in each class

public classA : IInterface
{
//implementation goes here 
}


And the calling step (if you want to retrieve the object only)
public IInterface Retrieve(string str)
      {
          return Bob[str];
      }

Or of if you want to retireve the dictionary
public Dictionary<string,IInterface  > Retrieve(string str)
      {
          return new Dictionary<string, IInterface>().Add(f, Bob[str]);
      }


Or for better solution use Linq (my favourite one )
public Dictionary<string,IInterface> Retrieve(string str) 
        {
 return Bob.Where(c => c.Key == str ).ToDictionary(c => c.Key, c => c.Value);
}


hope i could help
 
Share this answer
 
v2
:-D Yes interface usage is the answer.. I just realized it on my way into work.. :)

I've started a Article about this, and hopefull will have it up soone thanks.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900