Wednesday, July 25, 2007

How to enumerate Generic Dictionary .NET list

At first i found that enumerating a .net Dictionary class seem to be difficult but later i found that there is a way of doing this for example:

//instantiate a dictionary list
Dictionary _PointDic = new
Dictionary();
public Dictionary
PointDic
{
get { return _PointDic; }
set { _PointDic = value; }
}
//add value to named key
public void AddPoints(int point, string
ruleName)
{
int temp = 0;
if (!_PointDic.TryGetValue(ruleName, out
temp))
{
_PointDic.Add(ruleName,
point);
}
else
{
_PointDic.Remove(ruleName);
_PointDic.Add(ruleName,
point);
}
}
//sum up the values in the dictionary list
public int
CalculatePoints()
{
int totalpoints = 0;
foreach
(KeyValuePair s in _PointDic)
{
totalpoints +=
(int)s.Value;
}
return totalpoints;
}

3 comments:

Anonymous said...

You have really great taste on catch article titles, even when you are not interested in this topic you push to read it

Anonymous said...

I really like when people are expressing their opinion and thought. So I like the way you are writing

Anonymous said...

I read about it some days ago in another blog and the main things that you mention here are very similar