Creating unique identifiers with Flex

by Peter deHaan on November 1, 2007

in UIDUtil

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 comment… read it below or add one }

1 Nicolas August 15, 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.

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can 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

Previous post:

Next post: