Share via


can a Dictionary be the return type of a method?

Question

Monday, September 3, 2012 11:52 AM

Hi, I have the following code which retrieves distinct values of the field MainPageType, and then loops on this set, to retrieve the number of records of each MainPageType.

what I would like to return is a dictionary where the MainPageTypes are the keys, and the number of records per each is the value.

I get the same error message at all three segments I underscored: "EXPECTED CLASS, DELEGATE, ENUM, INTERFACE OR STRUCT".

what is wrong??

   public Dictionary<int, int> GetNumberOfRecordsPerMPT()
    {
        Dictionary<int, int> RecPerMPTdic = new Dictionary<int, int>();

        var z = (from x in db.T_MainPageDatas
                 where x.IsActive == true
                 select new { x.MainPageType }).Distinct();

        if (z != null)
        {
            foreach (var y in z)
            {
                int temp = Convert.ToInt32(y);
                RecPerMPTdic.Add(temp, CountRecordsOfType(temp));
            }
            return (RecPerMPTdic);
        }
        else return (null);
    }

    public int CountRecordsOfType(int t)
    {
        return (from x in db.T_MainPageDatas
                where x.IsActive == true && x.MainPageType == t
                select x).Count();
    }

Pushy.

All replies (5)

Monday, September 3, 2012 12:00 PM âś…Answered | 3 votes

All methods must be defined inside a class.


Monday, September 3, 2012 11:57 AM

This should work fine. Do you have a 'using System.Collections.Generic;' somewhere at the top of the .cs file?


Monday, September 3, 2012 12:08 PM | 1 vote

As Louis already said

namespace ConsoleApplication2
{
    public class Test
    {
        public Dictionary<int, int> method()
        {
            // this will work.
        }
    }

    public Dictionary<int, int> method2()
    {
        // this wont work, for that reason throw this error.
    }
}

Web Developer


Monday, September 3, 2012 12:12 PM

Yes, I do. Still, the problem exists...

Pushy.


Monday, September 3, 2012 12:17 PM

Goshhh, you are right, I had one many '}' before the method, which I have overlooked and actually "closed" the class. Thanks :)

Pushy.