4

Is there an easy way to get a count on the values of a specific dictionarys keys values?

static void Main()
{
    Dictionary<string, ArrayList> SpecTimes = new Dictionary<string, ArrayList>;
    ArrayList times = new ArrayList();
    string count = "";

    times.Add = "000.00.00";
    times.Add = "000.00.00";
    times.Add = "000.00.00";

   string spec = "A101";

   SpecTimes.Add(spec,times);

   count = SpecTimes[spec].values.count;
}
3
  • 1
    That looks like half C# and half... I don't know. Commented May 19, 2011 at 15:07
  • Do not use ArrayLists. Instead, use List<T> Commented May 19, 2011 at 15:07
  • Why are you mixing generic collections and non-generic collections? And not using brackets in your method calls? Please show real code. Commented May 19, 2011 at 15:08

5 Answers 5

4

I haven't tested it, but this should be close to what you need.

static void Main()
{
  Dictionary<string, List<string>> SpecTimes = new Dictionary<string, List<string>>();
  List<string> times = new List<string>();
  int count = 0;

  times.Add = "000.00.00";
  times.Add = "000.00.00";
  times.Add = "000.00.00";

  string spec = "A101";

  SpecTimes.Add(spec,times);

  // check to make sure the key exists, otherwise you'll get an exception.
  if(SpecTimes.ContainsKey(spec))
  {
      count = SpecTimes[spec].Count;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

3

There are some errors in your code, so it would not compile anyway. You should change it like this:

static void Main()
{
    IDictionary<string, IList<string>> specTimes = new Dictionary<string, IList<string>>();
    IList<string> times = new List<string>();

    times.Add("000.00.00");
    times.Add("000.00.00");
    times.Add("000.00.00");

    string spec = "A101";
    specTimes.Add(spec, times);

    int count = specTimes[spec].Count;
}

Since you already get the number of occurences, what is the problem anyway?

Comments

2

Your code isn't going to compile as is, and you shouldn't be using ArrayList, but rather List<T> (as SLaks pointed out.) That being said, List<T> has a Count property, so SpecTime[key].Count should work just fine (assuming key is actually in the dictionary.)

3 Comments

I know that you meant to say Dictionary<TKey, TValue> has a Count property.
@Rick It does, but why would that be what I mean?
Reading too fast. Mea culpa. (SpecTime.Count vs. SpecTime[key].Count)
1

If you're using .NET 3.5 and above, use Linq for this:

var count = (from s in SpecTimes where SpecTimes.Key == <keyword> select s).Count();

anyway, as everyone suggested, you should choose List<string> over ArrayList

Comments

1

If using .NET 3.5 you can use Linq to filter and count. However avoid ArrayList if possible, and use generics.

    static void Main(string[] args)
    {
        Dictionary<string, List<string>> SpecTimes = new Dictionary<string, List<string>>();
        List<string> times = new List<string>();
        int count;

        times.Add("000.00.00");
        times.Add("000.00.00");
        times.Add("000.00.00");
        times.Add("000.00.01");

        string spec = "A101";

        SpecTimes.Add(spec,times);

        // gives 4
        count = SpecTimes[spec].Count;

        // gives 3
        count = (from i in SpecTimes[spec] where i == "000.00.00" select i).Count();

        // gives 1
        count = (from i in SpecTimes[spec] where i == "000.00.01" select i).Count();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.