ASP.NET Basics (Part 5): Cooking Up a Storm
(Page 1 of 8 )

Learn all about arrays, enumerations, and the "foreach" loop, and find out how you can use them for more complex data storage and manipulation. Harish Kamath takes us through a tour of ASP.NET in this fifth of a nine part series. Here's a clip:
"Another interesting type of complex variable in C# is the so-called enumeration, or 'enum', type, which allows you to restrict the value of a variable to a list of pre-defined values."Another interesting type of complex variable in C# is the so-called enumeration, or "enum", type, which allows you to restrict the value of a variable to a list of pre-defined values. Here's what it looks like:
enum variable
-name {list of values}
And here's an example:
<%
enum Months { January, February, March, April, May, June, July, August, September, October, November, December }; %>
In order to use the enumeration, you need to define a variable of that type and assign it a value, as below.
<%
Months myBirthMonth = Months.July;
%>
Try as you might, C# will not allow you to assign a other value other than those listed in the enumeration to the variable "myBirthMonth".
The next example demonstrates the use of the "Months" enumeration in a simple C# script.
<SCRIPT language=c# runat="server">
enum Summer { April = 4, May = 5, June = 6, July = 7};
void Page_Load()
{
DateTime date = System.DateTime.Now;
Summer current_month;
current_month = (Summer) date.Month;
if (Enum.IsDefined(typeof(Summer), current_month))
{
output.Text = "Summer time! Get to work on that tan!";
}
else
{
output.Text = "It ain't summer, bud, so no point stocking up on suntan lotion";
}
}
</SCRIPT>
<?xml:namespace prefix = asp /><asp:label id=output runat="server"></asp:label>
Here is what the output looks like if it's summer,
and here's what it looks like when it's not.
In this example, I've used a "Summer" enumeration to create a set of values representing the four summer months of the year.
Typically, each value in an "enum" is associated with an integer. By default, the compiler will automatically assign an integer value, starting with 0 for the first value. But this does not really suit my purposes on two counts: first, there is no such thing as a 0th month and second, I need the month to be assigned a number corresponding to its correct position in the calendar. Therefore, I have explicitly defined integer values for each month.
<%
enum Summer { April = 4, May = 5, June = 6, July = 7};
%>
Next, the current month (an integer value, by default) is obtained from the system, and cast into the "Summer" enum data type.
<%
DateTime date = System.DateTime.Now;
Summer current_month;
current_month = (Summer) date.Month;
%>
Finally, the IsDefined() method of the "Enum" object is used to check if the value assigned to the "current_month" variable exists in the definition for the "Summer" enumeration. Don't worry too much about this, I'll be discussing objects and methods a little later on in this series. For the moment, all you need to know is that the IsDefined() method returns true if the value exists in the enumeration, and false if it does not; the output will change accordingly.
Next: Over and Out >>
More ASP.NET Articles
More By Harish Kamath (c) Melonfire