0

Here is my code:

package
{
import flash.display.Sprite;

public class MyClass extends Sprite
{
    private var abc:String = "123";
    public function MyClass()
    {

    }

    public function myfunc():void
    {
        dispatch("456");
    }

    private function dispatch(abc:String):void
    {
        trace(abc);
    }
}
}

When call the myfunc() function the trace will return 456. How can I get it to access to the global variable?

Thank you.

1
  • first of all why are you using the same name for argument variable and global variable? Commented Dec 2, 2013 at 9:26

2 Answers 2

4

Use this keyword:

private function dispatch(abc:String):void
    {
       trace( this.abc );
    }
Sign up to request clarification or add additional context in comments.

Comments

1

First of all, private var abc:String = "123"; is not global variable. It's a private member variable of the class. It's scope is the full class. When you add parameter in a member method with the same name, that parameter has the local scope in that method and that local parameter variable hides the class member of the same name.

private function dispatch(abc:String):void {
    // here abc has local scope and it hides the class member abc
}

You have two options to solve this:

  1. Simply use a different name for the parameter. For example, private function dispatch(ab:String):void. Now ab is local variable, abc is class member variable.
  2. If you must use the same name for parameter then use this.abc to access the class member.

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.