Vs 2008
This document describes how to use ASP. NET Ajax core library to construct enumeration types.
1. Create an enumeration
Now I create an enumeration, indicating the nationality type. registernamespace ("Tristan ");
Tristan. Nationality = function (){
Throw error. notimplemented ();
}
Tristan. Nationality. Prototype = {
China: 1,
USA: 2,
UK: 3
}
Tristan. Nationality. registerenum ("Tristan. Nationality ");
SYS. application. policyscriptloaded ();
Steps:
1) define a function named Tristan. Nationality
2) To avoid being instantiated, an error. notimplemented () exception is thrown when the function is called.
3) Enumeration items are defined in prototype. The enumerated value must be int type.
4) register the enumeration by calling Tristan. Nationality. registerenum.
2. Test
Write the test code: // declare a variable
VaR nationality = Tristan. Nationality. USA;
SYS. Debug. Trace (nationality );
// Call tostring () method to get the Enum key
SYS. Debug. Trace (Tristan. Nationality. tostring (nationality ));
SYS. Debug. Trace (Tristan. Nationality. tostring (3 ));
// Call parse () method to get the enum Value
SYS. Debug. Trace (Tristan. Nationality. parse ("China "));
Note:
Call the tostring () method and accept a value to obtain the key of the corresponding enumeration item.
Call the parse () method and accept the key to obtain the value of the corresponding enumeration item. The key parameter is case sensitive.
Output:
2
USA
UK
1