Tuesday, October 28, 2008

Remoting through interfaces

When we are going to use Remoting, we always think about the proxy classes, which should surely be deployed with the client-app. But is that the best approach?
The OOP tells us to understand the interaction through the intercaces, this means that components should interact only by contracts. Why don’t we use this approach in Remoting. This is surely possible. And this is better approach.
Let’s review this part of code:

public class RemotedObject:MarshalByRefObject
{
public void SomeMethod()
{
//… some code here
}
}

Let’s update the code above and add a contract of interacting with it:

public class RemotedObject:MarshalByRefObject, IRemotedObjectContract
{
public void SomeMethod()
{
//… some code here
}
}

//The interface is defined in a new module
public interface IRemotedObjectContract
{
void SomeMetod();
}

The remoted object now can be called through IRemotedObjectContract interface. But we should place the interface definition in a separate module, so it will be referenced from the main (RemotedObject) assembly, and also it will be referenced from the client. So now, the client have not to generate a proxy class by hand and attach that module, but it will be enough just to reference the interface-owner module and all is ready. Notice, that nothing should change on the server side. But on the client side for accessing the object we will have something like:


//…
IRemotedObjectInterface tmpRemote = (IRemotedObjectInterface)Activator.GetObject(typeof(IRemotedObjectInterface), tmpUrl);
//Use the retrieved tmpRemote interface to interact with the remote object


So, this is all, and this is really good approach in .Net Remoting.

Sunday, October 26, 2008

Implementing "C/C++" [union] in .Net

To improve performance, the CLR is capable of arranging the fields of a type any way it chooses. When you define a type, you can tell the CLR whether it must keep the type's fields in the same order as the developer specified them or whether it can reorder them as it sees fit.
The System.Runtime.InteropServices.StructLayoutAttribute attribute is used to tell the CLR what to do (how to layout the fields). You can pass LayoutKind.Auto as a value to the above specified attribute to have the CLR arrange the fields, or LayoutKind.Sequential, to have the CLR preserve the given layout, or LayoutKind.Explicit, to explicitly arrange the fields in memory by using offsets.
Microsoft's C# compiler selects LayoutKind.Auto for reference types, and LayoutKind.Sequential for value types.
As was mentioned above, the StructLayoutAttribute also allows you to explicitly indicate the offset of each field by passin LayoutKind.Explicit to its constructor. Then you apply an instance of the System.Runtime.InteropServices.FieldOffsetAttribute attribute to each field passing to this attribute's constructor an Int32, indicating the offset of the field's first byte from the beginning of the instance in bytes. Here is an example:


[StructLayout(LayoutKind.Explicit)]
public struct CUnionAlternate
{
[FieldOffset(0)]
byte byteField;
[FieldOffset(0)]
short shortField;
}

Friday, October 24, 2008

Optimizing Garbage Collection

Weak References


There is a way to affect the performance of the garbage collection, which is introduced in .Net through the WeakReferences.
When an object points to another one, this is called strong reference, or just reference, as we used to say, and in this case the GC will not collect that obhject as a "garbage". The WeakReferences are kind of references, the objects, which they point to can be collected, and if later they will be accessed throug the WeakReference, the access will fail.
The managed heap contains two internal data structures whose sole purpose is to manage weak references: short and long weak reference tables.
If an object has a short weak reference to itself, and is collected, then it's finalization method doesn't run, and it is being collected immediately. For the long weak reference, when the garbage collector collects object pointed to by the long weak reference table only after determining that the object's storage is reclaimable. If the object has a Finalize method, the Finalize method has been called and the object was not resurrected.
These two tables simply contain pointers to objects allocated within the managed heap. Initially, both tables are empty. When you create a WeakReference object, an object is not allocated from the managed heap. Instead, an empty slot in one of the weak reference tables is located; short weak references use the short weak reference table and long weak references use the long weak reference table.

Generations


Since garbage collection cannot complete without stopping the entire program, it can cause pauses at arbitrary times during the execution of the program. Those pauses can also prevent programs from responding quickly enough to satisfy the requirements of real-time systems.
One of the improvments of the GC is called generations. A generational garbage collector takes into account two facts:
  • Newly created objects tend to have short lives.
  • The older an object is, the longer it will survive.

Those collectors group objects by “age” and collect younger objects more often than older objects. All new objects added to the heap can be said to be in generation “0”, until the heap gets filled up which invokes garbage collection. As most objects are short-lived, only a small percentage of “young“ objects are likely to survive their first collection. Once an object survives the first garbage collection, it gets promoted to generation “1”. Objects, which are created after some generation stage are considered as on the “0” generation. The garbage collector gets invoked next only when the sub-heap of generation “0” gets filled up. All objects in generation “1” that survive get compacted and promoted to generation “2”. All survivors in generation “0” also get compacted and promoted to generation “1”. Generation “0” then contains no objects, but, as already was mentiond”, all newer objects after GC go into generation “0”.
Generation “2” is the maximum generation supported by the runtime's garbage collector. When future collections occur, any surviving objects currently in generation 2 simply stay in generation “2”.
Thus, dividing the heap into generations of objects and collecting and compacting younger generation objects improves the efficiency of the basic underlying garbage collection algorithm by reclaiming a significant amount of space from the heap and also being faster than if the collector had examined the objects in all generations.
This is all about garbage collectors, which, I think every, .Net developer must know.

Thursday, October 23, 2008

Understanding the Garbage Collection

One of the most important parts of the .Net framework is the Garbage Collector. Instead of letting you to take care of all the memory you have used, it automatically collects all unnecessary "garbage" when it's time. But how? This is the question I am going to discuss in this article.

The .NET CLR (Common Language Runtime) requires that all resources be allocated from the managed heap. The developer never need to free objects from the managed heap -they are automatically freed when they are no longer needed by the application. When the garbage collector runs, it checks for objects in the managed heap that are no longer needed by the application and performs the necessary operations to reclaim their memory. Because each type of the .Net is described by its Metadata, the garbage collector always knows, how to free up the unnecessary memory.
So, the garbage collector starts its job by locating the roots of the application.

The roots are:
  • all the global and static pointers
  • local variable pointers (which are on a thread's stack)
  • registers, which contain pointers to objects in the managed heap
  • pointers to the objects from the Freachable queue

The list of active roots is being maintained by the JIT compiler and CLR, and is made acceptable to the GC algorithm.
The GC works in two phases. Let's review them in detail.

The first phase (Marking)

(when the GC starts, it makes an assumption, that all the objects in the heap are garbage)
  1. identification of roots
  2. building the live object graph (GC runs through the roots and identifies live objects using the metadata of the objects)


To avoid the cycling process, if the GC tryes to add an object to the graph, which is already there, then the path, which by the object was found, is being ignored, and no more down-level searches are being made.
When the check for all the roots is done, all the alive objects are being added into the graph, so any other objects, surely, is garbage.

And now comes the second phase (Compacting).

  1. GC walks through the heap linearly looking for garbage blocks of memory.
  2. GC shifts non-garbage objects down in memory (of course, by updating all the references to the moved objects), making them easy reachable by removing all the gaps in the heap

After this phase the pointer is set to the end of the last object in the heap, referencing the place, where new object can be allocated on heap.

Finalization


Whenever a new object, having a Finalize method, is allocated on the heap a pointer to the object is placed in an internal data structure called Finalization queue. When an object is not reachable, the garbage collector considers the object garbage. The garbage collector scans the finalization queue looking for pointers to these objects. When a pointer is found, the pointer is removed from the finalization queue and appended to another internal data structure called Freachable queue, making the object no longer a part of the garbage. At this point, the garbage collector has finished identifying garbage. The garbage collector compacts the reclaimable memory and the special runtime thread empties the freachable queue, executing each object's Finalize method.
The next time the garbage collector is invoked, it sees that the finalized objects are truly garbage and the memory for those objects is then, simply freed.
It is recommended to avoid using Finalize method, unless required. Finalize methods increase memory pressure by not letting the memory and the resources used by that object to be released, until two garbage collections. Since you do not have control on the order in which the finalize methods are executed, it may lead to unpredictable results.
In my next article I will try to explain, how to optimize the GC.

Monday, October 6, 2008

Displaying IFrame contents in DIV containers

There are a lot of situations, when we need to show a web-content from another URL on our web-site. The standard solution for these situations is the IFrame tag. But it's not good approach to use IFrame, because it's resource consuming approach. The better approach, of course, is to use DIV container, and show the needed content in it.
That is not hard in 90% of situations, but there are special cases, when we really need help.


The problem There is a flash movie on the target webpage, which takes flashvars as input, and uses them. But when we use the simple ([DIV].innerHTML = [Iframe].innerHTML) mechanism, we get for example the links not work, or some other issues with those movies.


The thing is, that when we are getting the OBJECT tag content using the innerHTML property, we get it modified, but not the original content. And the problem is that the modified content removes the "flashvars" attribute content.


The solution is to use some kind of ruse. Instead of using the innerHTML property to get all the inner PARAM objects, which represent the parameters, we are getting them directly through the document's getElementsByTag() method call, and passing "PARAM" as an argument. This returns the original list of params, which then can be applyed to the new created object element using document.createElement("object") syntax.


But there is one more situation left. Huge number of these movies use events, which are handled by the webpages. And those scripts are being defined in the header part of the page.
To be sure that those cases are also fixed, we should read all the script tags from the header of the page, and add them (dynamically creating new script tags on our document object, to make the browser to run through the code instructions, not to ignore them) to the header of our document.


So, wish now you will never face this problem.

Wednesday, September 3, 2008

Removing list items in AS2/AS3


Let's suppose, we have some kind of custom list, elements of which has UI representation. A big problem for the beginner developers is that when they remove the items from the list, some of them still stay there, on the stage, and function normal. They are being removed only after several remove function calls.
Here is a code snippet describing such a situation:


public class TestClass{
private var items:Array;
public function RemoveItems():void{
for (var i:int = 0; i < this.items.length; i++){
RemoveItem(this.items[i]);
}
}

public function RemoveItem(argItem:Item):void{
var tmpIndex:int = this.items.indexOf(argItem);
if (tmpIndex != -1){
this.items.splice(tmpIndex, 1);
}
}
}



This usually happens, when the developer has already written a RemoveItem function, which also removes some event handlers or cast appropriate events on Remove. And when the developer needs to clear the list he wish to use that function. That is right. But the thing here is, that using the first iteration in RemoveItems function we will only iterate through about the half of all the items. The indexer i will grow up, but the same time the length of the items list will decrease, removing each item from it.
Sometime this causes big problems.

The best way to avoid this situation, is to use non length-dependent iteration. That is, i.e. while.
So if we just change the for (...) with while (this.items.length > 0){... we will win.

Monday, August 4, 2008

Module-Oriented development


One of the most important aspects of the development process is the flexibility. Most developers think, that flexibility is also affects to the core of the process, but that also affects the UI of the application.
There are situations, which in some parts (modules) of the application UI stop responding, and all the other UI elements stop responding because of that.
This is just an example, when we need module-oriented development as the main idea for whole the development process.
Let's start from a simple example. So we have a UI application. First of all we can define virtual blocks of UI, each of which should represent a module. For example, let's suppose we should have a search part and toolbar part on the GUI. But our application should not contain all that stuff in it. Each part should be represented by a module. So we should have a Search Module, and a Toolbar Module. Each module should be represented by a separete assembly.
And our application should have a configuration file (xml), where it should keep paths for all installed modules. When it should start, it should just go through the records in the config file. For each module it should give a GUI container, where each module muust be drawn.
During the initialization the application should initialize all the modules first and then start. Using this approach should give us ability to connect different parts of our GUI to different assemlyies. So if for exampl we have not some assemly, it should just not load. This also gives the main application ability to control all the modules work, by giving them appropriate security rules.
Also we should be able to unload the modules, which work bad, during runtime and be error - free.
To gain this, we must define an interface, which should be implemented by each Module. The application should work only with the interface for modules.

The module-oriented development applies not only to GUI applications, but to any type of applications, which cann be represented as multi-module structure.

Simple Socket Server


The idea of creating a good socket-server depends on several factors, which should surely be calculated in accordance to the requirements of the given task.
The most simple socket server exampl is the "One-Thread" server, which does all the job in its main thread. But this is not good enought for system, which should give a lot of time to each connected client to process.
Here come the threads. In this case, each client has its own (separete) thread, which in it works, and does its tasks as long as it need. Here is the main idea of most client-server applications.
The example project represents a simple Socket-Server, which keeps track of all connected clietns. Each client is represented by a ConnectionUser class instnace on the server. That class has a single "Run" method, which starts the processor of that client in a separete thread.
The project is a VS.Net 2005 Solution, but I have also included the source files in the post.


##########################################################################
############################SimpleServer.cs###############################

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;

namespace SimpleSocketServer
{
public class SimpleServer
{
///
/// The port, to be listened for connection requests
///

private int portToListen = -1;

///
/// The listener object, which through the clients should connect
///

private TcpListener listener;

///
/// Keeps the state of the server
///

private bool isStarted = false;

///
/// The list of connected users
///

private List users = new List();

///
/// Creates an instance of a SimpleServer class
///

public SimpleServer()
{

}

///
/// Creates an instance of a SimpleServer class configured for listening the given port
///

/// The port to be listened
public SimpleServer(int argPortToListen)
{
this.PortToListen = argPortToListen;
}

///
/// Starts the server on the pre-specified port.
/// If the port was not specified, throws an Exception
///

public void Start()
{
if (portToListen == -1)
{
throw new Exception("No port specified for the listener");
}

isStarted = true;
listener = new TcpListener(this.portToListen);
try
{
listener.Start();
while (isStarted)
{
try
{
//Get the TcpClient instance for the connection
TcpClient tmpNewClient = listener.AcceptTcpClient();

//Pass the retrieved object to the new processing unit
//that unit in a new threads
ConnectionUser tmpNewUser = new ConnectionUser(tmpNewClient);

tmpNewUser.ConnectionClosed += new EventHandler(ConnectionUser_ConnectionClosed);

lock (this.users)
{
//add the new user to the track list
this.users.Add(tmpNewUser);
}

//Start processing the client in a new thread
tmpNewUser.Run();
}
catch (Exception ex)
{

}
}
}
catch (Exception e)
{
//Handler code here
}
}

///
/// Handles the ConnectionClosed event of each connected user
///

/// The sender of the event
/// The event arguments
protected void ConnectionUser_ConnectionClosed(object sender, EventArgs e)
{
lock (this.users)
{
ConnectionUser tmpUser = sender as ConnectionUser;
tmpUser.ConnectionClosed -= new EventHandler(ConnectionUser_ConnectionClosed);
if (this.users.Remove(sender as ConnectionUser))
{
Console.WriteLine("Client disconnected");
}
}
}

///
/// Stops the server instance
///

public void Stop()
{
this.isStarted = false;
this.listener.Stop();

foreach (ConnectionUser tmpUser in users)
{
tmpUser.Stop();
}
}

///
/// Gets or sets the port
///

public int PortToListen
{
get
{
return this.portToListen;
}

set
{
if (value <= 0)
{
throw new ArgumentOutOfRangeException();
}

this.portToListen = value;
}
}
}
}


##########################################################################
###########################ConnectionUser.cs##############################
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.IO;

namespace SimpleSocketServer
{
///
/// The class, which uses a single TcpClient
/// connection for communication with remote host
///

public class ConnectionUser
{
public event EventHandler ConnectionClosed;

protected TcpClient connection;
private bool isStarted = false;

public ConnectionUser(TcpClient argClient)
{
this.connection = argClient;
}

///
/// Starts a new Thread and runs current instance in that thread
///

public void Run()
{
Thread tmpNewThread = new Thread(new ThreadStart(StartProcessing));
tmpNewThread.Start();
}


///
/// The processor method, which keeps whole logic of the processor unit
/// Must be overriden in all descendant classes.
///

protected virtual void StartProcessing()
{
isStarted = true;
}

///
/// Gets the Network stream of the underlying connection
///

protected Stream NetworkStream
{
get
{
return connection.GetStream();
}
}


///
/// Stops the new-thread processor
///

public void Stop()
{
this.isStarted = false;
this.connection.Close();
}

///
/// Handles the connection close events
///

private void OnConnectionClose()
{
if (this.ConnectionClosed != null)
{
this.ConnectionClosed(this, null);
}
}
}
}

Thursday, July 24, 2008

String Trees: an interview question


Different people are finding creating a parser quite a hard job. But the main “ring” in it is to understand not some development problem, but is to find a good representation (format) for the serializer and deserializer. This means that the hardest part of the parser is to find good delimiters for the task.
So let’s begin from creating a parser, for a simple binary tree. Let’s suppose that each node of that tree can have two (left and right) child nodes, and also a value. Then let’s choose some simple format for serialization. Here is a simple example.

{LeftChild|Value:RightChild}

This will surely work, if our content (values of nodes) does not contain any characters like “:” or “{”, “}”.

And now let’s look at a simple example:
{{{|4:}|3:{|5:{|2:}}}
Let’s understand the above written example. The root node of the tree has a value of 3. It has a left child, which has value of 4 and has no child nodes – it’s a leaf node. The right child of the root node is a node, with a value of 5. It has no left child, but has a right child, which has a value of 2. And that node is also a leaf node. So now we finally have an appropriate imagination about the example given above.
Now we can create a parser for it very easy. Most starting developers are trying to use recursive functions to create parsers, but they usually miss an important aspect, that if in that case the (for example) tree, which they are going to parse is too big, they will get a “Memory overflow” exception. This means that the best way to create a parser is to use sequential parsing algorithm.

Here is the code for the upper example on C#.
public class TreeNode
{
    public const char NODE_START = '{';
    public const char NODE_END = '}';
    public const char LEFT_SEPARATOR = '|';
    public const char RIGHT_SEPARATOR = ':';

    private int value;
    private TreeNode leftChild;
    private TreeNode rightChild;
    private TreeNode parentNode;

    public static TreeNode Parse(string argString)
    {
        //The format for the input node is:
        //{[leftChild]|value:[rightChild]}

        TreeNode tmpCurrentRoot = null;
        int tmpPosition = 0;
        ParseStage tmpStage = ParseStage.LeftChild;
        while (tmpPosition < argString.Length)
        {
            switch (argString[tmpPosition])
            {
                case NODE_START:
                    //Create a new node with the current parent
                    TreeNode tmpNewNode = new TreeNode(tmpCurrentRoot);
                    if (tmpCurrentRoot != null)
                    {
                        if (tmpStage == ParseStage.LeftChild)
                        {
                            tmpCurrentRoot.LeftChild = tmpNewNode;
                        }
                        else if (tmpStage == ParseStage.RightChild)
                        {
                            tmpCurrentRoot.RightChild = tmpNewNode;
                        }
                    }
                    //Set the pointer of current node to the new created one
                    tmpCurrentRoot = tmpNewNode;
                    tmpStage = ParseStage.LeftChild;

                    tmpPosition++;
                    break;

                case LEFT_SEPARATOR:
                    tmpStage = ParseStage.Value;
                    tmpPosition++;
                    break;

                case RIGHT_SEPARATOR:
                    tmpStage = ParseStage.RightChild;
                    tmpPosition++;
                    break;

                case NODE_END:
                    if (tmpCurrentRoot.ParentNode == null)
                    {
                        return tmpCurrentRoot;
                    }
                    else
                    {
                        if (tmpCurrentRoot == tmpCurrentRoot.ParentNode.LeftChild)
                        {
                            tmpStage = ParseStage.LeftChild;
                        }
                        else
                        {
                            tmpStage = ParseStage.RightChild;
                        }
                        tmpCurrentRoot = tmpCurrentRoot.ParentNode;
                    }
                    tmpPosition++;
                    break;

                default:
                    if (tmpStage == ParseStage.Value)
                    {
                        string tmpValueStr = argString.Substring(tmpPosition, argString.IndexOf(RIGHT_SEPARATOR, tmpPosition) - tmpPosition);
                        tmpCurrentRoot.Value = Int32.Parse(tmpValueStr);
                        tmpPosition += tmpValueStr.Length;
                        tmpStage = ParseStage.RightChild;
                    }
                    break;
            }
        }

        throw new Exception("ERROR DURING PARSING");
    }

    public TreeNode(TreeNode argParentNode)
    {
        this.parentNode = argParentNode;
        leftChild = null;
        rightChild = null;
        value = 0;
    }

    public int Value
    {
        get
        {
            return this.value;
        }
        set
        {
            this.value = value;
        }
    }

    public TreeNode LeftChild
    {
        get
        {
            return this.leftChild;
        }

        set
        {
            this.leftChild = value;
            if (this.leftChild != null)
            {
                this.leftChild.parentNode = this;
            }
        }
    }

    public TreeNode RightChild
    {
        get
        {
            return this.rightChild;
        }
        set
        {
            this.rightChild = value;
            if (this.rightChild != null)
            {
                this.rightChild.ParentNode = this;
            }
        }
    }

    public TreeNode ParentNode
    {
        get
        {
            return this.parentNode;
        }
        set
        {
            this.parentNode = value;
        }
    }

    public override string ToString()
    {
        string tmpResult = String.Empty;
        tmpResult += NODE_START;
        if (this.leftChild != null)
        {
            tmpResult += this.leftChild.ToString();
        }
        tmpResult += LEFT_SEPARATOR;
        tmpResult += this.value;
        tmpResult += RIGHT_SEPARATOR;

        if (this.rightChild != null)
        {
            tmpResult += this.rightChild.ToString();
        }

        tmpResult += NODE_END;
        return tmpResult;
    }

    protected enum ParseStage
    {
        LeftChild,
        Value,
        RightChild
    }
}

Tuesday, June 10, 2008

Singleton pattern realisation

This is the correct implementation of the Singleton design pattern. It is easier to not have the lazy-initialization option, but if you have to then go on with the following example.
And here is it's implementation on C#.

public class SingletonTest
{
  private static SingletonTest instance;
  private static readonly Object syncRoot = new Object();
 
  private SingletonTest ()
  {
  }

  public static SingletonTest Instance
  {
    get
    {
      if (instnace == null)
      {
        lock (syncRoot)
        {
          if (instance == null)
          {
            instance = new SingletonTest();
          }
        }
      }
     
      return instance;
    }
  }
}

Saturday, May 17, 2008

Repeating processes and animations

During the work we meet some problems with repeating some processes or animations periodically. For exampl we have some animation, which we want to loop or some process. Here is the implementation for it. I will first write down the actionscript 2 implementation of it using non-documented Animation class, which is just a wrapper of a movieClip, with more functionality like start(), stop() and which casts "COMPLETED" event.
We should write a class for dynamically repeat the animation until the stop method hasn't been called.


//AS 2.0 implementation
class AnimRepeater{

    private var _isStarted : Boolean;

    private var _clipName : MovieClip;

    private var _animationLoop : Animation;



    public function AnimRepeater(clipName : String) {

        _clipName = clipName;

    }



    public function start() : Void {

        if (!_isStarted) {

           _isStarted = true;

           updateAnim();

        }

    }



    public function stop() : Void {

        if (_isStarted) {

           _isStarted = false;

        }

    }



    private function updateAnim() : Void {

       if (_isStarted) {

           var autoDelete : Boolean = true;

           _animationLoop = new Animation(_clipName, "animName", autoDelete);

           _animationLoop.addEventListener(Animation.FINISHED, Delegate.create(this, updateAnim));

           _animationLoop.start();

        }

    }


}




For C# let's add some flexibility. We should use a IProcess interface, which should be used to controll the process which should be repeated.

public delegate void ProcessEventHandler(IProcess argProcess, EventArgs argEventArgs);

interface IProcess

{

    public event ProcessEventHandler Finished;

    public void start();

    public void stop();

}



public class ProcessRepeater : IProcess

{

    protected bool isStarted;

    protected IProcess processToRepeat;

    public event ProcessEventHandler Finished;



    public ProcessRepeater(IProcess argProcess)

    {

       processToRepeat = argProcess;

       isStarted = false;

    }



    public void start()

    {

       if (!isStarted)

       {

          isStarted = true;

          updateProcess();

       }

    }



    public void stop()

    {

       if (isStarted)

       {

          isStarted = false;

          processToRepeat.stop();

          processToRepeat.Finished -= new ProcessEventHandler(updateProcess);

       }

    }



    protected void updateProcess()

    {

       if (isStarted)

       {

          processToRepeat.Finished += new ProcessEventHandler(updateProcess);

          processToRepeat.start();

       }

    }



    private void updateProcess(IProcess argProcess, EventArgs argEventArgs)

    {

       updateProcess();

    }

}

Tuesday, May 13, 2008

Enumerations in ActionScript 2.0

Here is an alternate for Enumerations in ActionScript 2.0. Just add an item with needed name as a member to the class (as _NORMAL or _ENUM_ITEM) and then use them through the "get" functions from another code.

/**
* @author Artak Mkrtchyan
*/
class EnumTypeName extends Number
{
private static var _NORMAL:EnumTypeName = EnumTypeName(0);
private static var _ENUM_ITEM:EnumTypeName = EnumTypeName(1);

private function EnumTypeName(value:Number){

}

public static function get NORMAL() : EnumTypeName {
return _NORMAL;
}

public static function get _ENUM_ITEM() : EnumTypeName {
return _ENUM_ITEM;
}
}


////////////////////////////////
//And here is the usage of this

//.... another class
public function test():Void{
var testVar:EnumTypeName;

testVar = EnumTypeName.NORMAL;
}
//.... other code