Search code examples
c#snmp

SNMP#Net KeyValuePair error in sample code?


I am using the simple example and have the following code:

using SnmpSharpNet;
using System;
using System.Collections.Generic;

namespace SNMP
{
    class Program
    {
        static void Main(string[] args)
        {
            string host = "10.65.10.17";
            string community = "public";
            SimpleSnmp snmp = new SimpleSnmp(host, community);

            if (!snmp.Valid)
            {
                Console.WriteLine("SNMP agent host name/ip address is invalid.");
                return;
            }
            Dictionary<Oid, AsnType> result = snmp.Get(SnmpVersion.Ver1,
                                                      new string[] { ".1.3.6.1.4.1.11.2.3.9.4.2.1.1.16.1.1.1.26.0" });
            if (result == null)
            {
                Console.WriteLine("No results received.");
                return;
            }

            foreach (KeyValuePair kvp in result)
            {
                Console.WriteLine("{0}: {1} {2}", kvp.Key.ToString(),
                                      SnmpConstants.GetTypeName(kvp.Value.Type),
                                     kvp.Value.ToString());
            }
        }
    }
}   

when trying to build i get

Using the generic type 'System.Collections.Generic.KeyValuePair<TKey,TValue>' requires 2 type arguments 

am i missing a reference somewhere? or does the sample need tweaking for .net 4.5 and VS2013?

Thanks


Solution

  • You can either supply the type parameters:

    foreach (KeyValuePair<Oid,AsnType> kvp in result)
    {
       Console.WriteLine("{0}: {1} {2}", kvp.Key.ToString(),
                    SnmpConstants.GetTypeName(kvp.Value.Type),
                    kvp.Value.ToString());
    }
    

    Or let the compiler perform type inference:

    foreach (var kvp in result)
    {
       Console.WriteLine("{0}: {1} {2}", kvp.Key.ToString(),
                    SnmpConstants.GetTypeName(kvp.Value.Type),
                    kvp.Value.ToString());
    }