Documentation for: ASP.NET Ajax Version 1.0

This documentation is for a previous version. For the current released version, see the ASP.NET Ajax documentation on MSDN.

 

 

 

 

 

 

 

Type.registerEnum Method

Registers a class as an enumeration.

Syntax

ANamespace.AnEnum.registerEnum(name, flags);

Arguments

name

A string that represents the fully qualified name of the enumeration.

flags

true to indicate that the enumeration is a bit field; otherwise, false.

Exceptions

Exception Type Condition

Error.argument

(Debug) name cannot be evaluated as a class.

Error.invalidOperation

(Debug) name can be evaluated as a class, but it is not the type of this instance.

-or-

A value of the enumeration is not an integer.

-or-

A value name of the enumeration cannot be registered because it is a reserved name.

Remarks

Use the registerEnum method to register a class as an enumeration (enum). An enum is a distinct type that consists of a set of named integer constants. Each enumerator must have a value that evaluates to an integer. The value can be expressed as a hexadecimal value such as 0xFFFFFF. The value name of the enum must not be a reserved keyword or name.

If the enum is a collection of bit-field flags, specify true for the flags parameter.

note

Assigning additional values to new versions of an enum or changing the values of the enum members in a new version can cause problems for consumers of the enum. Enum values are often used in switch statements, and if additional elements have been added to the enum type, the test for default values can return true unexpectedly. It is important to provide guidelines on how the code should react if new elements are added to any enum types.

Example

The following example shows how to define an enumeration and register it by using the registerEnum method.

JavaScript

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
       <asp:ScriptManager runat="server" ID="ScriptManager1">
       </asp:ScriptManager>
       <script type="text/javascript">
            // Register classes to test.
            Type.registerNamespace('Samples');

            // Define an enumeration type and register it.
            Samples.LineType = function(){};
            Samples.LineType.prototype = 
            {
                Solid:      0,
                Wide:       1,
                Rounded:    2
            }
            Samples.LineType.registerEnum("Samples.LineType");

            // Define an enumeration type and register it.
            Samples.Color = function(){};
            Samples.Color.prototype = 
            {
                Red:    0xFF0000,
                Blue:   0x0000FF,
                Green:  0x00FF00,
                White:  0xFFFFFF 
            }
            Samples.Color.registerEnum("Samples.Color");

            // Define an enumeration flags type and register it.
            Samples.Permissions = function(){};
            Samples.Permissions.prototype = 
            {
                Create: 0x0, 
                Read:   0x2,
                Update: 0x4,
                Delete: 0x8
            }
            Samples.Permissions.registerEnum("Samples.Permissions", true);


            // Output: "1".
            alert(Samples.LineType.Wide);

            // Output: "16711680".
            alert(Samples.Color.Red);

            // Output: "4".
            alert(Samples.Permissions.Update);

        </script>
    </form>
</body>
</html>