1

Possible Duplicate:
Dictionary With Value “Variable”

I have some highly coupled code that I am trying to decouple. If I could make a Dictionary like this:

Dictionary<string, var> dict = new Dictionary<string, var>

a lot of coupling could be fixed. One poster in another thread seems to have found a solution using a Variable class but he/she didn't post any details of this class and I can't think of how to do it.

EDIT:

Basically, instead of:

string transit = <string value>;

I would like to be able to write:

string dict["Transit"] = <string value>;

Where I have already done:

Dictionary<string, var> dict = new Dictionary<string, var>()
{
    {"Transit", transit}
};

This probably isn't possible but any advice is appreciated.

Regards.

Community
  • 1
  • 1
Kevin
  • 1,252
  • 6
  • 32
  • 50
  • 6
    Difficult to give you a constructive answer without seeing the code you want to decouple - you are proposing a solution instead of giving us the problem and having us propose solutions. – Oded Aug 17 '12 at 15:58
  • 3
    honestly, it's hard to say what *exactly* you're asking about.. – Tigran Aug 17 '12 at 15:59
  • Unclear. Looks like you want to store something by reference but we'd need much more context. – H H Aug 17 '12 at 15:59
  • I just edited my post to show why I am trying to achieve. – Kevin Aug 17 '12 at 16:01
  • All I see is a normal class field - why would putting it into a `Dictionary` help? It is still unclear from your post what problem you are trying to solve. – Oded Aug 17 '12 at 16:01
  • 1
    I think you're misunderstanding the var keyword first. var is simply a declarative shortcut. If you don't know the type of value being stored, use object, with the understanding that you'll have to cast the return values and boxing may be a performance concern (in extreme cases). In that case, dmck's answer is perfectly valid. – Chris Aug 17 '12 at 16:02
  • 3
    is it a dupplicate post of your other question http://stackoverflow.com/questions/12008203/dictionary-with-value-variable ? – user287107 Aug 17 '12 at 16:04

2 Answers2

6

This would do the trick:

public class Variable
{
    public object Value { get; set; }
}

Then he could do what he is asking, namely:

Dictionary<string, Variable> dict = new Dictionary<string, Variable>()
{
    {"Transit", new Variable()}
}

dict["Transit"].Value = "transit"
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Schemer
  • 1,635
  • 4
  • 19
  • 39
  • Thanks for the answer (you're right, I was assuming it was more complicated) and thanks for the support. What bugs me about the downvoters is that they don't leave a comment. I mean, it's a forum, if you can't be constructive what are you doing here? – Kevin Aug 20 '12 at 14:34
0

I assume you want to add even IEnumerable,IQueryable variables to your dictionary. Object class is the super class and can store anything and can be cast to anything.

var myVar = new { name="john",lastname="smith"}; // or any linq expression
Dictionary<string, Object> asd = new Dictionary<string, object>();
asd.Add("mykey", myVar);
Erhan A
  • 691
  • 11
  • 21