views:

2232

answers:

5

HI i have a main class

//main.as

package {
    public class main {
     public var testGlobal:string = "testValue";

    }
}


//pop.as

package {
    public class pop {
     function pop():void {
      trace("testGloabl from main.as" + testGlobal);
     }
    }
}

How can i get the testGlobal value on pop.as width out using a main class Object. Is there any method of Global variables??

How to use global variables in AS3 .

+4  A: 

testGlobal is not a global variable, it is a public instance variable of the Main class. You cannot access it without using a Main class object, because without an object there is no existence for a property. Instance variables are tied to objects.

If you want to access create a property that is not tied to any particular object instance, declare it as static.

//Main.as
package {
    public class Main {
        public static var testGlobal:string = "testValue";
    }
}


//Pop.as
package {
    public class Pop {
        function pop():void {
                trace("testGloabl from Main.as" + Main.testGlobal);
        }
    }
}
Amarghosh
ok That i know, but i want a gloabl variable to set value from one class and get or access from diffrent class.. how is it possible??
coderex
You can get/set the public static variable `Main.testGlobal` from any class.
Amarghosh
then how can i set a global variable in as3
coderex
If by `set` you mean to modify the value, this is how you do it `Main.testGlobal = "some other value"` - If you are looking for global variables in general, again, making a variable `public static` is the way to go.
Amarghosh
how can i create a Global variable in AS3, is AS3 support Global variables like in AS2 ??
coderex
I am not familiar with AS2, but in AS3 `public static` variables are the closest you can get to global vars.
Amarghosh
Not quite Amarghosh, my solution is closest to a global variable, albeit a weird one :)
alecmce
+2  A: 

If you absolutely positively have to have a global variable in as3, you could always create a file in the top-level of your source folder like this:

MULTIPLIER.as

package
{
    public var MULTIPLIER:int = 3;
}

Then, whenever you need your multiplier you could reference wherever you need it like this:

DoSomeMultiplying.as

package multiplying
{
    public class DoSomeMultiplying
    {
        public function multiplyMe(n:int):int
        {
            var m:int = n * MULTIPLIER;
            MULTIPLIER = m;
            return m;
        }
    }
}

However, I would strongly recommend that you do not do this! it is horribly bad practice, horribly slow and, well, just horrible!

But there it is, it is possible to create a global variable or constant in the default package to act as a global constant or variable.

alecmce
I agree with alecmce THIS IS A HORRIBLE PRACTICE!
unkiwii
A: 

You can use the static, like it was said before, or you can use the Singleton pattern. There are no private constructors in AS, so what you can do is: a) be very carefull not to call the constructor b) send an exception everytime someone calls the constructor and the instance has already been set.

I don't think there are actual global variables - but I don't see why you would need any. If you want a variable in class to be modifiable from the outside just declare it static then do className.variableName = .

webdreamer
@webdreamer, yes there are, if you must have them. I refer you to my post, which you appear to have ignored!
alecmce
Well I said I don't think there are... That's because I'm not sure... All I wanted to do is to refer to the Singleton pattern as a pattern that is sometimes used to circumvent the problems of using global variables... I don't remember the specific day this was (10 days ago!) though I do remember your answer. I probably only noticed it after I posted mine.
webdreamer
A: 

I agree with what the others just said, avoid global variables, prefer constant (and usually constants are not meant to be changed)

just_a_dude
A: 

Thanks for this post.

Ela
That's not an answer, that's a comment - you should add it as a comment to whichever post was helpful, or under the question.
alecmce