Skip to main content
Known Participant
April 18, 2010
Answered

Variable scope

  • April 18, 2010
  • 2 replies
  • 1333 views

Hello the one thing I do not get is variable scope. Below is some fully functional code.

Where I have the  comment  ( // public or private ?)  is what I do not get.

I tried to change to change var to public var or to private var on both of those lines

of code and each time I get errors. So what exactly is the scope since you cannot specify

public or private as a prefix to either line?

package {
    import flash.display.Sprite;
    import flash.events.*;
    import flash.media.Microphone;
    import flash.system.Security;
    import flash.system.SecurityPanel;

    public class MicrophoneExample extends Sprite {
        public function MicrophoneExample() {
            var mic:Microphone = Microphone.getMicrophone();  // public or private ?
            Security.showSettings(SecurityPanel.MICROPHONE);
            mic.setLoopBack(true);
                   
            if (mic != null) {
                mic.setUseEchoSuppression(true);
                mic.addEventListener(ActivityEvent.ACTIVITY, activityHandler);
                mic.addEventListener(StatusEvent.STATUS, statusHandler);
            }
        }

        private function activityHandler(event:ActivityEvent):void {
            trace("activityHandler: " + event);
            var str:String="Test";  // public or private ?
            trace(str);
        }

        private function statusHandler(event:StatusEvent):void {
            trace("statusHandler: " + event);
        }
    }
}

Thamks,

Jim

This topic has been closed for replies.
Correct answer kglad

all variables prefixed with var inside a function body are local to the function in which they're declared during that particular function call.  ie, you can't expose that variable so it makes no sense to use private, public etc.

2 replies

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
April 18, 2010

all variables prefixed with var inside a function body are local to the function in which they're declared during that particular function call.  ie, you can't expose that variable so it makes no sense to use private, public etc.

jimfid45Author
Known Participant
April 19, 2010

Thanks to both of you. KGlad I see the difference

the way you described it however despite the fact it is

local meaning to me private to that function, I just felt that

using private would be considered by the compiler to be

redundant and not a cause for an error.

Example a Function

if a function does not return a variable whether you

specify void or not generates no error.

Jim

kglad
Community Expert
Community Expert
April 19, 2010

there are some things the compiler checks and some things it doesn't even check.    but all declarations (private, internal public or otherwise) would all be wrong.

the variable is not private.  private variables are accessible to the class in which they're declared.  a variable declared local to a function is not accessible outside the function even if that's still within the class scope.

Inspiring
April 18, 2010

Hello Jim.

I'm still learning as3 but, as far as I know, you just declare vars as public/private/protected when they are inside your class and outside your constructor and/or methods, and I can't find any vars outside your constructor...

I'm sure someone else may explain this in more details to you and even correct me if I'm wrong.

See ya