views:

174

answers:

5

Despite if it is good practice or not, I read here that you can have package variables (or constants), so I tried this:

// globals.as
package global
{
    public const someConst:String = 'theValueOfTheConst';
    public var someVar:String = 'theValueOfTheVar';
}

// SomeClass.as
package pack.to.the.class
{
    // ...
    import global.*;
    // ...

    // ...
    public function aFunction():void
    {
        trace(someConst);
        trace(someVar);
    }
    // ...
}

And all I have is an Compile-time Error that says "Definition of global:someConst has not been found" (the same for someVar)

I'm using Flex and I see this in Problems. So, is this possible? Can I have package variables (or constants) without using a Class?

Thanks!

PS: The package names, the variables names and function names are all an example, I use other names when I tested.

A: 

I severely doubt it... and if you could I will suspect it's more effort than simply making a global class.

Jasconius
I always do that. One global class. But I see this and want to know if it's poissible.
unkiwii
A: 

For what it's worth, I tried testing it out myself and couldn't get it to work either, even when the variables and the references to them were in the same package.

Wesley Petrowski
A: 

Certainly possible, but discouraged. In your source/root (main source folder in the compiler settings) put your global class:

package {

public const someConst:String = 'theValueOfTheConst';

}

(oh... and I'm using flex to test, but she worked -- no need to import to get someConst in a different package locale)

jeremym
But i'm doing exactly that and didn't work..
unkiwii
Maybe the Flex compiler works differently, but in Flex (and maybe you can translate this into as3) there is a "source path" setting. This is the folder upon which the package naming originates (if you default all development to the "src" folder, and you have a "com" folder inside of src, the package names inside of the com folder look like "package com { }". So, for me, if I put global.as in the main source folder, and type it the way written above, I don't need to ever import someConst.But, like I said... Flex. Probably completely different for you.Best of luck!
jeremym
see Joa Ebert answer, maybe you name the global.as file as someConst.as (that's was my problem, the name of the file)
unkiwii
A: 

if you change

public const someConst:String = 'theValueOfTheConst';

to

public static const someConst:String = 'theValueOfTheConst';

then it works for me. YMMV.

Bart Alcorn
Or I'm doing something wrong or I'm very stupid.. because didn't work for me.
unkiwii
+2  A: 

The answer is quite simple. You may just have one definition in your file. Split them into two files and it works.

And your file has to be named exactly like the variable. So in your example, this would work:


//someConst.as
package global
{
    public const someConst:String = 'theValueOfTheConst';
}

//someVar.as
package global
{
    public var someVar:String = 'theValueOfTheVar';
}

Each "compilation unit" (fancy term for *.as file) may have only one visible definition. You can also not put two classes into the same file. However you may have as many anonymous definitions as you want.

Joa Ebert
And that's the problem.. Thanks!
unkiwii