tags:

views:

42

answers:

3

I am trying to created nested dictionary variable like the below, But I get compile error stating that it needs "}" at line where I am adding items (line #2) to my nested dictionary.

What Am I missing here? Thanks.

Dim myNestedDictionary As Dictionary(Of String, Dictionary(Of String, Integer)) = New Dictionary(Of String, Dictionary(Of String, Integer))()


myNestedDictionary.Add("A", New Dictionary("A", 4)())
+1  A: 

You need to specify the type of dictionary you are creating when you add the records:

myNestedDictionary.Add("A", New Dictionary(Of String, Integer))

or otherwise pass an existing Dictionary(Of String, Integer) as the inside-dictionary argument (when adding key/value pairs to the external dictionary).

(BTW, Your external dictionary is a dictionary who's Keys are Strings and Values are Dictionaries (Of String, Integer), is this really what you wanted?)

M.A. Hanin
I am trying to store value like Blossum62 (I cannot paste the matrix here), so that during my code implementation I can get values like : [A,A] = 4, [A,R] = -1, [A,N] = -2, [A,D] = -2, [A,C] = 0, [A,Q] = -1 [R,A] =1, [R,R] = 5 and so on. ------------
anshu
There are several ways to implement this, which may prove to be a better solution than the nested dictionary. This goes a bit outside the scope of this question, you may want to consider posting a new question which concerns what you are trying to achieve (rather than your implementation attempts).
M.A. Hanin
Thanks M.A. Hanin. I will post a new question.
anshu
I have posted a new question at http://stackoverflow.com/questions/2449283/best-and-simple-data-structure
anshu
And I see you got several answers and quite a few vote-ups! *thumbs up*
M.A. Hanin
+1  A: 

In VS 2008 and .net 3.5 you cannot declare and initialize a Dictionary in one line, so you have to do:

 Dim myNestedDictionary As New Dictionary(Of String, Dictionary(Of String, Integer))()
 Dim lTempDict As New Dictionary(Of String, Integer)
 lTempDict.Add("A", 4)
 myNestedDictionary.Add("A", lTempDict)

To retrieve the an item use the following:

Dim lDictionaryForA As Dictionary(Of String, Integer) = myNestedDictionary.Item("A")
Dim lValueForA As Integer = lDictionaryForA.Item("A")

The value in lValueForA should be 4.

Ando
Thanks for your reply. I was able to get rid of the error. Can you please suggest something how to retrieve values from this nested dictionary, for example I want to get what is the match value for A and A, which is 4 in this case.For the below, case, it will what is match value for A and r, which is -1.lTempDict.Add("R", -1) myNestedDictionary.Add("A", lTempDict)
anshu
I using VS 2008
anshu
I edited the post to illustrate how to also retrieve an item. Hope it clears things up for you.
Ando
+1  A: 

In C# you can do that:

var myNestedDictionary = new Dictionary<string, Dictionary<string, int>>() {{ "A", new Dictionary<string, int>() { { "A", 4 } } }};

You can do it in VB 2010 as well, using the From keyword, but it doesn't compile in VS 2008. It will compile in VB 2010, no matter which .NET Framework you target. I've tried 2.0, 3.5 and 4.0.

Dim myNestedDictionary = New Dictionary(Of String, Dictionary(Of String, Integer))() From {{"A", New Dictionary(Of String, Integer) From {{"A", 4}}}}
Richard Hein