tags:

views:

14

answers:

2

hello experts My goal is to create a generic function that selects a value in a combobox accoring to a value. (My comoBox holds arrayCollection as dataProvider.)

The difficulty is infact to get a propertyname in runtime mode

public function selectComboByLabel(combo:ComboBox , propetryName:String, value:String):void {
    var dp:ArrayCollection = combo.dataProvider as ArrayCollection;
    for (var i:int=0;i<dp.length;i++) {
        if (dp.getItemAt(i).propertyName==value) {
            combo.selectedIndex = i;
            return;
        }
    }
}

the line if (dp.getItemAt(i).propertyName==value) is of course incorrect. It should be arther something like: dp.getItemAt(i).getPropertyByName(propertyName)

Any clue on how to that ?

+2  A: 

Don't use Object Property notation. Do this:

dp.getItemAt(i)[propertyName]
www.Flextras.com
+1  A: 

In addition to what Flextras said, you could also redo your for loop to make it easier to read:

for each(var item:Object in dp) {
      if(item[propertyName] == value) {
          combo.selectedItem = item;
          return;
      }
  }
bedwyr