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.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
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()
Use
public Dictionary<string, string> loopThroughNotificationCountQueries() { ... }
or explain why that's not possible.
public IDictionary<string, string> loopThroughNotificationCountQueries()
{
var countQuery = new Dictionary<string, string>(); ...
... return countQuery;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With