01
Nov
07

Creating unique identifiers with Flex

The following example shows you how you can create a unique indentifier (UID) with Flex using the UIDUtil class in the mx.utils package. By calling the static UIDUtil.createUID() method, you can easily create a hexadecimal string in the following format: “XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX”.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/11/01/creating-unique-identifiers-with-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="generateUID();">

    <mx:Script>
        <![CDATA[
            import mx.utils.UIDUtil;

            private function generateUID():void {
                lbl.text = UIDUtil.createUID();
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Button id="btn"
                label="Create UID"
                click="generateUID();" />
    </mx:ApplicationControlBar>

    <mx:Label id="lbl"
            fontSize="20"
            selectable="true" />

</mx:Application>

View source is enabled in the following example.

Note that the generated unique identifier is not a globally unique value, but rather a pseudo-random number generator based on the current time.


1 Response to “Creating unique identifiers with Flex”


  1. 1 Nicolas Aug 15th, 2008 at 8:34 pm

    I’m loving flexexamples. With about 3/4 of the things I google, I end up here.

    So an overdue comment: thanks a lot.

    N.

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".