Adam
Adam

Reputation: 2682

use id as variable

I'm passing a component id to a function and then using the id as a variable.

For example the id I'm passing is - user_dg

Issue I'm have is when I do a trace on the variable - this[dg_id]

I get - Application0.Canvas662.Canvas878.DataGrid888, when I'd like it to be - user_dg

How can I output the actual component id?

Upvotes: 0

Views: 138

Answers (1)

JeffryHouser
JeffryHouser

Reputation: 39408

Based on the comments; It sounds like you're passing the wrong thing into the function; most likely an instance of the component instead of the actual component's name.

It's a bit vague because you didn't show us the method or the way you're calling it. But, something like this:

public function myFunction(component:UIComponent):void{
  trace(component);
  trace(component.id);
}

Will expect an instance of the component, not the actual component. You might call this function like this:

myFunction(myDataGrid);
myFunction(myList);
myFunction(myComboBox)

If you want to pass in the ID; it would be unusual, but you might do something like this:

public function myFunction(componentID:String):void{
 trace(this[componentID]);
 trace(componentID);
}

And you could call it like this:

myFunction('stringThatRepresentsAVariableName');
myFunction(myDataGrid.id);
etc..

It is highly unusual to use the ID over the actual instance you need to process. Requiring an instance provides better documentation for the person who needs to maintain this code at some future point. Requiring the string ID necessitates the need that the function is in the same component that has the ID as a child; which may decrease your re-use or refactoring opportunities in the future.

Upvotes: 1

Related Questions