I have a dictionary, where both keys and values are my own classes. How can I get an array of keys? How can I find out what keys are in this dictionary?
Asked
Active
Viewed 2,385 times
3 Answers
6
var dict = new Dictionary();
dict["a"] = 1;
dict["b"] = 2;
var arrResult: Array = new Array();
for ( var key: Object in dict )
{
arrResult.push( key );
}
trace( arrResult ) -> b,a

Azzy Elvul
- 1,403
- 1
- 12
- 22
1
You can use lamda expression for the same. Here is the sample code for it.
Dictionary<string, string> Test = new Dictionary<string, string>();
Test.Add("Azhar","Mansuri");
Test.Add("Azhar2", "Mansuri");
Test.Add("Azhar3", "Mansuri");
Test.Add("Azhar4", "Mansuri");
Test.Add("Azhar5", "Mansuri");
Test.Add("Azhar6", "Mansuri");
string[] key = Test.Select(s => s.Key).ToArray();
key array will return all the keys of dictionary.
Update : since you want to know the key there is no better way than this:
for (var k:Object in dictionary) { var value:ValType=dictionary[k]; var key:KeyType=k; // do stuff }
It is just a sample code. However it may be helpful for you.

Azhar Mansuri
- 665
- 1
- 7
- 22
-
1What's wrong woth you? peaple?! :) I'm asking about ActionScript 3, but 1st of you giving answer on Objective-C, 2ns - on C#, 3rd - on C# too! Are you kidding me?! :) – SentineL Dec 20 '12 at 08:45
-
1You didn't mention that you wanted to make it in ActionScript 3 in your question.Please check my modified answer. – Azhar Mansuri Dec 20 '12 at 08:52
-2
look at the link for action script
Just use for ios
NSArray*keys=[dict allKeys];
In general, if you wonder if a specific class has a specific method, look up Apple's own documentation. In this case, see NSDictionary class reference. Go through all the methods. You'll discover many useful methods that way.

HDdeveloper
- 4,396
- 6
- 40
- 65
-
@SentineL ok you didn't mentioned, take alook at [link](http://stackoverflow.com/questions/7997199/getting-keys-by-values-in-a-dictionary) – HDdeveloper Dec 20 '12 at 08:37