user1035479
user1035479

Reputation: 83

C# Dictionary Return Type

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.

Upvotes: 8

Views: 69065

Answers (8)

Tony Hopkinson
Tony Hopkinson

Reputation: 20320

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.

Upvotes: 0

Henk Holterman
Henk Holterman

Reputation: 273209

Use

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

or explain why that's not possible.

Upvotes: 9

Ron Sijm
Ron Sijm

Reputation: 8738

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> first.

Upvotes: 1

Ilya Smagin
Ilya Smagin

Reputation: 6152

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;
}

Upvotes: 1

fixagon
fixagon

Reputation: 5566

yes its because you dont specify the return type.

two possibilities:

the better: you specify the return type to Dictionary

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

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500225

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()

Upvotes: 13

alexl
alexl

Reputation: 6851

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


        ... return countQuery;
    }

Upvotes: 4

AndrewC
AndrewC

Reputation: 6730

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() 

Upvotes: 2

Related Questions