Tipo de retorno del diccionario de C #

I've got a problem with some c# code I'm writing, I'm fairly new to c# and I've had a look around and can't find a solution.

I've got a method that returns a Dictionary, I've set the return type to object and it seems ok.

    public object loopThroughNotificationCountQueries()
    {
        var countQuery = new Dictionary<string, string>(); ...


        ... return countQuery;
    }

The problem is in the main method where I'm trying to loop through the elements returned from the dictionary.

                Notification notification = new Notification();

                var countDictionary = notification.loopThroughNotificationCountQueries();


                foreach(KeyValuePair<String, String> entry in countDictionary)
                {
                    ...
                }

I'm getting an error saying "Error 2 foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'"

Is it because I'm not specifying the correct return type for a dictionary? Or is there another way of iterating through the entries in the returned object?

Thanks for your help, Stephen.

preguntado el 08 de noviembre de 11 a las 11:11

8 Respuestas

Look at your method declaration:

public object loopThroughNotificationCountQueries()

Eso significa tu countDictionary declaration is effectively:

object countDictionary = notification.loopThroughNotificationCountQueries();

... and you can't use foreach con una object like that. The simplest fix is to change the method declaration, e.g. to

// Note case change as well to follow .NET naming conventions
public IDictionary<string, string> LoopThroughNotificationCountQueries()

respondido 08 nov., 11:15

Thanks for your help, as I said I'm new to C# and I wasn't aware that methods could be declared as a dictionary. - user1035479

Utilizan

public Dictionary<string, string> loopThroughNotificationCountQueries() { ... }

or explain why that's not possible.

respondido 08 nov., 11:15

@Vlad - What access modifier? Do you mean return type? - Oded

@Vlad - I know what access modifiers are. I just don't understand why you think they are relevant to the answer. - Oded

public IDictionary would be better. (instead of Dictionary) - Ron Sijm

@Oded Admittedly not very stackoverflowy of me, but I found Henk's typo amusing. The answer is valid too, so I stand by my +1. - Vlad

public IDictionary<string, string> loopThroughNotificationCountQueries()
    {
        var countQuery = new Dictionary<string, string>(); ...


        ... return countQuery;
    }

respondido 08 nov., 11:15

Is there a reason you can't have your method signature as below? Do you always return a dictionary with a string key type and a string data type?

public Dictionary<string, string> loopThroughNotificationCountQueries() 

respondido 08 nov., 11:15

tu loopThroughNotificationCountQueries devoluciones object. Make it return Dictionary<string, string> by changing its signature.

public Dictionary<string, string> loopThroughNotificationCountQueries()
{
    var countQuery = new Dictionary<string, string>(); ...


    ... return countQuery;
}

respondido 08 nov., 11:15

yes, it's suppose to be:

public IDictionary<string, string> loopThroughNotificationCountQueries()
{
}

You can only itterate through objects of IEnumerable<T>

so if for some reason you cannot change loopThroughNotificationCountQueries, cast the object to an IDictionary<string, string> de antemano.

respondido 08 nov., 11:15

yes its because you dont specify the return type.

dos posibilidades:

the better: you specify the return type to Dictionary

the worse: you cast the object to a dictionary in the calling method

respondido 08 nov., 11:15

He does specify a return type - object. - Oded

ha ha. like i write in the abstract of a book: "its a book" yes its correct - arreglar

If here were not to specify a return type, he would have used void. - Oded

void would mean that the method declaration doesnt have a return type. but this one has one. its just not correct specified. ;-) - arreglar

You should return not a dictionary in non-private method, it's exposing the type, and all it's methods and properties, if you don't need them ,and you shouldn't in most cases, don't. Turn on FxCop and it will howl at you for doing this

Lots of ways round it, the chances of you wanting to do SomeClass.SomeDictionary.Add("name","value") are small, the chance of that being a sensible implementation are near non-existent.

In general I simply have my class have a private member of Dictionary Type and expose a few methods e.g.

public IEnumerable<String> Names { get { return _myDictionary.Keys;} }

etc.

If I'm doing it a lot, delegate to a simple class and carry that about.

respondido 08 nov., 11:17

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.