Home > DeveloperSection > Blogs > Enumerations in c#

Enumerations in c#


.NET C#  ASP.Net 
Ratings:
0 Comment(s)
 652  View(s)
Rate this:

Enumerations in c#

This blog explains how enumerations works in c#.

Definition:

Enum is a keyword that represent a value type for declaring a set of named constant.

An enum helps to define a series of related integral constants that represent special values within a module of code. An enum can be used in a switch statement, which is used as a decision-making statement for comparing numeric values.

Example:

1.       Create a Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="EnumExample.Default" %>

 

<!DOCTYPE html>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

 

        <table>

            <tr>

                <td colspan="2">Enter First Number</td>

                <td colspan="2">

                    <asp:TextBox ID="txtboxfirst" runat="server"> </asp:TextBox></td><td><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" ControlToValidate="txtboxfirst"></asp:RequiredFieldValidator>

                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="*" ValidationExpression="^[1-9]\d*(\.\d+)?$" ControlToValidate="txtboxfirst"></asp:RegularExpressionValidator></td>

            </tr>

            <tr>

                <td colspan="2">Enter Second number</td>

                <td colspan="2">

                    <asp:TextBox ID="txtboxsencond" runat="server"></asp:TextBox></td><td>

                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*" ControlToValidate="txtboxsencond"></asp:RequiredFieldValidator>

                    <asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ErrorMessage="*" ValidationExpression="^[1-9]\d*(\.\d+)?$" ControlToValidate="txtboxsencond"></asp:RegularExpressionValidator>

                </td>

            </tr>

            <tr>

                <td colspan="2">Result</td>

                <td colspan="2">

                    <asp:TextBox ID="txtResult" runat="server" Enabled="false"></asp:TextBox></td>

            </tr>

            <tr>

                <td>

                    <asp:Button ID="btnAdd" runat="server" Text="+" OnClick="btnAdd_Click" Width="49px" /></td>

                <td>

                    <asp:Button ID="btnSub" runat="server" Text="-" OnClick="btnAdd_Click" Width="49px" /></td>

                <td>

                    <asp:Button ID="btnMul" runat="server" Text="*" OnClick="btnAdd_Click" Width="49px" /></td>

                <td>

                    <asp:Button ID="btnDiv" runat="server" Text="/" OnClick="btnAdd_Click" Width="49px" /></td>

            </tr>

        </table>

    </form>

</body>

</html>

 

 

2.      Write in Default.aspx.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Collections;

namespace EnumExample

{

    public partial class Default : System.Web.UI.Page

    {

     

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

 

        enum Operation { Add = '+', Mul = '*', Sub = '-', Div = '/' }

        protected void btnAdd_Click(object sender, EventArgs e)

        {

            Button btn = (Button)sender;

 

//Convert button text to enum

            Operation type = (Operation)char.Parse(btn.Text);

            switch(type)

            { case Operation.Add:

                    txtResult.Text=Convert.ToString(Convert.ToDouble(txtboxfirst.Text) + Convert.ToDouble(txtboxsencond.Text));

                    break;

            case Operation.Div:

                    txtResult.Text = Convert.ToString(Convert.ToDouble(txtboxfirst.Text) / Convert.ToDouble(txtboxsencond.Text));

                    break;

            case Operation.Mul:

                    txtResult.Text = Convert.ToString(Convert.ToDouble(txtboxfirst.Text) * Convert.ToDouble(txtboxsencond.Text));

                    break;

            case Operation.Sub:

                    txtResult.Text = Convert.ToString(Convert.ToDouble(txtboxfirst.Text) - Convert.ToDouble(txtboxsencond.Text));

                    break;

 

        }

        }

    }

}

 

Output:

 

Advantage:

1.       In future you can easily change values.

2.       Your code can be read easier.

 


Don't want to miss updates? Please click the below button!

Follow MindStick