C# Dictionary Return Type
Solution 1
Look at your method declaration:
public object loopThroughNotificationCountQueries()
That means your countDictionary
declaration is effectively:
object countDictionary = notification.loopThroughNotificationCountQueries();
... and you can't use foreach
with an 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()
Solution 2
Use
public Dictionary<string, string> loopThroughNotificationCountQueries() { ... }
or explain why that's not possible.
Solution 3
public IDictionary<string, string> loopThroughNotificationCountQueries()
{
var countQuery = new Dictionary<string, string>(); ...
... return countQuery;
}
Solution 4
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()
Solution 5
your loopThroughNotificationCountQueries
returns object
. Make it return Dictionary<string, string>
by changing its signature.
public Dictionary<string, string> loopThroughNotificationCountQueries()
{
var countQuery = new Dictionary<string, string>(); ...
... return countQuery;
}
user1035479
Updated on November 09, 2020Comments
-
user1035479 over 3 years
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.
-
Oded over 12 yearsHe does specify a return type -
object
. -
Oded over 12 years@Vlad - What access modifier? Do you mean return type?
-
fixagon over 12 yearsha ha. like i write in the abstract of a book: "its a book" yes its correct
-
Vlad over 12 years@Oded Access Modifiers
-
Oded over 12 yearsIf here were not to specify a return type, he would have used
void
. -
Oded over 12 years@Vlad - I know what access modifiers are. I just don't understand why you think they are relevant to the answer.
-
Ron Sijm over 12 yearspublic IDictionary would be better. (instead of Dictionary)
-
Vlad over 12 years@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.
-
Oded over 12 years@Vlad - Didn't spot the typo. Good thing I could see the edit ;)
-
fixagon over 12 yearsvoid would mean that the method declaration doesnt have a return type. but this one has one. its just not correct specified. ;-)
-
user1035479 over 12 yearsThanks for your help, as I said I'm new to C# and I wasn't aware that methods could be declared as a dictionary.