concept enumeration in category .net

appears as: enumerations, n enumeration, enumeration
ASP.NET AJAX in Action

This is an excerpt from Manning's book ASP.NET AJAX in Action.

  • JavaScript extensions— As you’ll see in chapter 3, the library leverages the object model provided by JavaScript by introducing an enhanced type system that supports reflection and object-oriented constructs like classes, interfaces, and enumerations. In addition, the built-in JavaScript objects have been extended to support methods commonly found in .NET classes.
  • Together with interfaces, enumerations are a feature supported by many object-oriented languages. Because JavaScript doesn’t support them, let’s see how you can fill this gap using the Microsoft Ajax Library.

    Enumerations are a way to give names to numbers. The Microsoft Ajax Library lets you create enumerations to associate names with integer values. You can also specify values in hexadecimal format and create bit-field flags. Let’s start with the pattern for creating a Size enumeration, shown in listing 3.13.

    Listing 3.13. A Size enumeration
    Type.registerNamespace('Samples'); Samples.Size = function() { throw Error.notImplemented(); } Samples.Size.prototype = { Small: 1, Medium: 2, Large: 3 } Samples.Size.registerEnum('Samples.Size');

    As you did with interfaces, you define an enumeration by starting with a function. Because it makes no sense to instantiate an enumeration, you need to avoid the use of the Samples.Size function as a constructor. To do this, it’s enough to raise a client exception of type notImplemented as soon as someone attempts to call the function.

    The enumeration names are defined in the prototype object, and they must be integers. They can also be specified using the hexadecimal format. The name and value pairs in the prototype object are considered the names and values of the enumeration. In the example, you define three names (Small, Medium, and Large) associated with three integers (1, 2, 3). You call the registerEnum method, passing the fully qualified name of the enumeration as an argument.

    Let’s play a bit with enumerations to illustrate some of the methods available. Creating a variable of type Samples.Size is easy:

    3.5. Understanding interfaces and enumerations

    JavaScript doesn’t support interfaces and enumerations, but the Microsoft Ajax Library simulates these constructs using functions as it does classes. The pattern is similar: You declare a function and then upgrade it to an interface or an enumeration using the registerInterface or registerEnum method, respectively. In the same manner as registerClass, these methods store in a function various pieces of information that allow them to be treated as interfaces or enumerations rather than as simple functions. Let’s start by examining interfaces; enumerations will follow.