简体   繁体   中英

Creating a multidimensional dictionary in C# for Windows Phone 7.8

I need to store daily statistics in the isolated storage of Windpws Phone which leads me to believe that a multidimensional dictionary would be the most useful but I'm having trouble wrapping my head around the logic needed to get this to work.

The stats looks something like this in pseudocode:

dictionary DailyStats {
1,
[Stats] => dictionary {
  [Class] => 5,
  [Entertainment] => 3,
  [Personnel] => 2,
  [Price] => 7,
  [Quality] => 6
  }
}

I started out with this:

var DailyStats = new Dictionary<int, Dictionary<string, string>>();

But as soon as I wanted to assign values to this structure I got lost quite fast. The values are collected by the app for each day.

I've thought of Linq but it seems to be overkill for what I'm trying to do.

Can anyone point me in the right direction?

Thanks!

If you have one dicionary with StatusClasses ???

var DailyStats = new Dictionary<int, StatusClass>();

And:

class StatusClass
{
    //substitute the vars for their type
    var Class;
    var Entertainment;
    var Personnel;
    var Price;
    var Quality;

    public StatusClass(var ClassValue, var EntertainmentValue, var Personnel.....)
    {
        Class = ClassValue;
        Entertainment = EntertainmentValue;
        ...........
    }
}

If your keys are fixed Daniel's solution is the way to go. If you want to use a dictionary a static class like this might help:

static class DailyStats
{
    static Dictionary<int, Dictionary<string, string>> _dic;

    static DailyStats()
    {
        _dic = new Dictionary<int, Dictionary<string, string>>();
    }

    public static void Add(int i, string key, string value)
    {
        if (!_dic.ContainsKey(i))
            _dic[i] = new Dictionary<string, string>();
        _dic[i][key] = value;
    }

    public static string Get(int i, string key)
    {
        if (!_dic.ContainsKey(i) || !_dic[i].ContainsKey(key))
            return null;
        return _dic[i][key];
    }
}


    DailyStats.Add(1, "Stats", "a");
    Console.WriteLine(DailyStats.Get(1, "Stats"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM