tags:

views:

197

answers:

2

I see code like the following in several books and examples online. The problem is they all feed in a single method into a dictionary object which requires two.

IDictionary statistics = connection.RetrieveStatistics();

Which returns the IDE error:

Error   1 Using the generic `type 'System.Collections.Generic.IDictionary<TKey,TValue>' requires '2' type arguments C:\Users\SGM7\Documents\ASPX 3_5 book code\Pro ASP.NET 3.5\Chapter07\Website\Tester.aspx.cs 22 9 C:\...\Website\`

So even though connection.RetrieveStatistics() returns pairs, how can I get IDictionary to accept connection.RetrieveStatistics()?

Here's the code block.

connection.Open();
connection.StatisticsEnabled = true;

IDictionary statistics = connection.RetrieveStatistics();

lblBytes.Text = "Received bytes: " + statistics["BytesReceived"].toString();
connection.Close();
lblBytes.Text += connection.State.ToString();
+2  A: 

Right-click on RetrieveStatistics in your code block in VS and pick 'go to definition' to see how it is defined. My guess is it's a System.Collections.IDictionary, not a System.Collections.Generic.IDictionary<K,V>.

Or, if you're using .NET 3.5 or higher, use var:

var statistics = connection.RetrieveStatistics();
Jonathan
Ah yes! If I remove the "using System.Collections" I get the same compile error he describes. Good one!
Matt Hamilton
Thank you. Figured it had to be something simple, else why would there be so many examples using that code. I just fully qualified it and it ran.
+1  A: 

The compile error makes it looks like you're trying to use a generic dictionary instead of a non-generic Dictionary.

The return type of RetrieveStatistics() is IDictionary, not IDictionary<T,K>

Do you have a using declaration that's mapping IDictionary to something else? Have you tried using System.Collections.IDictionary explicitly?

For example:

System.Collections.IDictionary statistics = connection.RetrieveStatistics();

As Jonathan says, var will also work, because the compiler will just figure out what the type should be.

Nader Shirazie