1

Hi All is there any way to locally define a variable in a function and then pass it to the oher function. I mean to say is it possible the pass a local value from one function to other function. Somebody Please suggest me the solution. Thanks in advance

3 Answers 3

7

Or it's that simple or you meant something else:

private function function1():void
{
    var localVariable:String = "this is local variable of function1()";
    function2(localVariable);
}

private function function2(string:String):void
{
    trace(string);
}

function1();

or use global variable as temporary storage:

private var globalVariable:String = "";

private function function1():void
{
    var localVariable:String = "this is local variable of function1()";

    globalVariable = localVariable;
}

private function function2():void
{
    trace(globalVariable);
}

function1();
function2();
Sign up to request clarification or add additional context in comments.

1 Comment

Hi zdmytriv, Yes thats what i was wanted as a solution..Thanks a ton.!!
2

zdmytriv is right.

Although, you can also make default variables, like so:

(Modifying zdmytriv's code)

private function function1():void
{
    var localVariable:String = "this is local variable of function1()";
    function2(localVariable);
    function2(); //You don't have to enter a default argument
}

private function function2(string:String = "something else"):void
{
    trace(string);
}

This would trace:

this is local variable of function1()
something else

A little off topic, but good to know.

1 Comment

Thanx pipeep for such a nic explanation.
0

Primitives in Flex are passed by value, where complex objects are passed by reference. You can use this to pass objects around without scoping a variable outside the functions themselves. For instance:

private function function1():void {
{
     var localVar:Object = {value:"test"};
     trace(localVar.value);
     function2(localVar);
     trace(localVar.value);
}

private function function2(obj:Object):void
{
     obj.value = "new value";
}

This would trace:

test
new value

Which reflects the fact that function2 receives the parameter "obj" by reference, as a pointer to the original "localVar" object. When it sets the .value field, that change is reflected in function1.

I just thought I'd point that out.

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.