ApplicationsBlog

How to Define Global Constants in Flex

By July 2, 2009 September 5th, 2019 4 Comments

There are several ways to define a set of global constants in your Flex app. You can use a bunch of “include” statements or you can create a singleton object to hold your data. However, probably the best way to define a set of global constants is to create an object with all static constants as members. You don’t have to refer to any instances of the object, you don’t have to instantiate it, and the object will be available wherever you import it. Here is an example of a Global object:

Globals.as

package com.caspar.model{
    public class Globals {

        public static const EXAMPLE_1_TEXT:String =   "Some Text";
        public static const EXAMPLE_2_TEXT:String =   "Some More Text";

        public static const APP_WIDTH:Number =      1200;
        public static const APP_HEIGHT:Number =      800;
        public static const BORDER_SIZE:int =      13;
        public static const SIDE_PANEL_WIDTH:int =    330;
        public static const SIDE_PANEL_HEIGHT:int =   APP_HEIGHT - ( 2* BORDER_SIZE );
        public static const MAIN_PANEL_WIDTH:int =    APP_WIDTH - (SIDE_PANEL_WIDTH + ( 3 * BORDER_SIZE ) );
        public static const MAIN_PANEL_HEIGHT:int =   SIDE_PANEL_HEIGHT;

    }
}

As you can see here, you can strongly type your constants, and you can perform basic math and concatenations in your constant declarations. This can be very useful when you have a lot of constants defined – if you have defined your globals carefully, you should only have to change a few values. To use the object, import it as shown below: Global Object Usage:

<Group >
<Script>
<![CDATA[
import com.caspar.model.Globals;
]]>
</Script>
<Rect height="{Globals.SIDE_PANEL_HEIGHT}"/>
</Group>

Have questions? Let us know in a comment below, or contact our team directly. You can also check out our other posts on customizing your Salesforce solution.

Caspar Harmer

Caspar Harmer

Caspar is a New Zealander working for Soliant from far-off Wellington. He loves exploring new technologies and solving problems. Caspar also loves getting into the outdoors; he runs, mountain bikes and does a lot of orienteering when he can fit it in.

4 Comments

Leave a Reply