[ASP.NET AJAX Client] Enumerations

ASP.NET AJAX even simulates Enumerations. You are not surprised to hear that JavaScript does not support enumerations. Enumeratoins associate names with integer values (you can use hexadecimal formats). ASP.NET AJAX also lets you create bit-field flags. 

1. Creating an enumeration

Enumerations can be used with a namespace. To create an enumeration, you need to create a constructor function. It makes sense to make the constructor not to be called directly.

Type.registerNamespace('MyAjax');
MyAjax.Color = function () {
  throw Error.notImplemented();
}

2. Adding Names

Enumeration names are defined in the “prototype” object and must be integers.

MyAjax.Color.prototype = {
  Red : 1,
  Green : 10,
  Blue : 20
}

3. Registering an Enumeration

The “registerEnum()” method is used to register an enumeration.

MyAjax.Color.registerEnum('MyAjax.Color');

4. Using Enumerations

You can use an enumeration just as it is a type. When you refer one of values in an enumeration, the underlying integer value is used. To find out the name of a value, you can use the “<enumType>.toString(value)” method. You can also parse the string name as an enumeration value using the “<enumType>.parse(strValue)” method.

var greenColor = MyAjax.Color.Green;
alert(greenColor); // 10
alert(MyAjax.Color.toString(greenColor)); // Green

var blueColor = MyAjax.Color.parse('Blue');
alert(blueColor); // 20

5. Flags

Enumerations are good when you need to select only one option from a list. But what if multiple selection is possible? Flags can be used for multiple selection more efficiently.

  • In order for flags to work correctly, values must be powers of 2.
  • To enable the flag, you must pass “true” as a second argument to the “registerEnum” method
Type.registerNamespace('MyAjax');
MyAjax.Color = function () {
  throw Error.notImplemented();
}
MyAjax.Color.prototype = {
  Red : 1,
  Green : 2,
  Blue : 4
}
MyAjax.Color.registerEnum('MyAjax.Color', true);
  • Combine flags: use the bitwise OR (|) operator
  • Remove a flag: use the AND(&)-NOT(~) syntax
  • Check whther a flag is selected:  use the bitwise AND (&) operator
// Combine "Red" and "Blue"
var redBlue = MyAjax.Color.Red | MyAjax.Color.Blue;
alert(MyAjax.Color.toString(redBlue)); // Red, Blue
var isRedSelected = (redBlue & MyAjax.Color.Red) == MyAjax.Color.Red;
alert('Is Red selected: ' + isRedSelected); // true

// Remove Red
var blueColor = redBlue & ~MyAjax.Color.Red;
alert(blueColor); // 4
var isRedSelected = (blueColor & MyAjax.Color.Red) == MyAjax.Color.Red;
alert('Is Red selected: ' + isRedSelected); // false

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s