QueenOfError
QueenOfError

Reputation: 49

how to get count ArrayList in dictionary wpf c#

I want to get count of list but my list is in dictionary.

Dictionary < string,  ArrayList > ();

wording["utterance"+x].Count; // this gives me count for items in dictionary.

What I want to know is:

Upvotes: 0

Views: 730

Answers (3)

Dave Cousineau
Dave Cousineau

Reputation: 13168

how many items I are there in my ArrayList?

The code you gave should do just that:

// number of items in the ArrayList at key "utterance" + x
wording["utterance"+x].Count; 

how to refer to the elements in the list?

You can refer to them by index:

// get the 4th item in the list at key "key"
object myObject = wording["key"][3];

Or you can iterate over them:

foreach (object item in wording["key"])
   DoSomething(item);

To summarize, wording is a Dictionary that stores ArrayLists by a string key. You can retrieve a particular ArrayList by indexing with the appropriate string key for that ArrayList.

wording // evaluates to Dictionary<string, ArrayList>
wording["sometext"] // evaluates to ArrayList

Note that the latter will throw an exception if you have not already placed an ArrayList at that key.

Upvotes: 0

Tilak
Tilak

Reputation: 30698

how many items I are there in my ArrayList?

(wording["utterance"+x] as ArrayList).Count; // gives count of items in ArrayList

how to refer to the elements in the list?

wording["Actual Key"][<numeric index of item number>; // wording["utterance"+x][0] in your case for first item in arraylist

Upvotes: 0

Lloyd
Lloyd

Reputation: 29668

You could of course do:

ArrayList al = wording["key"];
int count = al.Count;

I'm curious why your initial code wouldn't work though, unless Linq extensions are interfering.

I would go with Amicable's suggestion of List<T> over ArrayList though.

Upvotes: 1

Related Questions