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.

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

2 Responses to Creating unique identifiers with Flex

  1. Nicolas says:

    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.

  2. Kyle Ward says:

    I would like to second what Nicolas said, i don’t know how often i end up on this site! Great resource, thanks!

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree