Using C# Enum in a switch case.

Using C# enum in a switch case

Using C# Enum in a switch statement can often result in cleaner code; also, it helps reduce the chances of any typo's creeping into your code.

In this post, I'm going to update the font size, font colour and section background colour by pulling the values from Umbraco and then use a C# switch case to compare the value with public Enum values.

I will not be using any break default values in this example. Still, you can add them if you require a break case.

I will not show the properties and doctype for the Umbraco CMS.

Code for setting up the C# Enum in a switch case.

To start, we need to create a controller, view model, helper class and view to get the values from the switch statement and display on the page.

using System.Text.RegularExpressions;
using System.Web.Mvc;
using Umbraco.Core.PropertyEditors.ValueConverters;
using Umbraco.Web;
using Umbraco.Web.Mvc;
using Web.Helper;
using Web.Helper.EnumHelper;
using Web.Model.ColourPicker;
namespace Web.Core.Controllers
{
    public class EnumExampleController : SurfaceController
    {
        public ActionResult Index()
        {
            var currentPage = CurrentPage.DocumentTypeAlias;
            var umbContent = UmbracoAssignedContentHelper.PageContentByAlias(currentPage);
            string textColour       = umbContent.GetPropertyValue("textColour").Label;
            string backgroundColour = umbContent.GetPropertyValue("sectionBackgroundColour").Label;
            int fontSize = umbContent.GetPropertyValue("fontSize");
            string displayTextColour        = "black";
            string displayBackgroundColour  = "white";
            switch (textColour)
            {
                case nameof(EnumColourPicker.ColourPickerEnum.Red):
                    displayTextColour = "textRed";
                    break;
                case nameof(EnumColourPicker.ColourPickerEnum.Black):
                    displayTextColour = "textBlack";
                    break;
            }
            switch (Regex.Replace(backgroundColour," ",""))
            {
                case nameof(EnumColourPicker.ColourPickerEnum.Tomato):
                    displayBackgroundColour = "backgroundColourTomato";
                    break;
                case nameof(EnumColourPicker.ColourPickerEnum.DarkOrange):
                    displayBackgroundColour = "backgroundColourDarkOrange";
                    break;
            }
            string displayfontSize = DisplayFontSize.SectionFontSize(fontSize);
            var model = new ColourPickerViewModel
            {
                TextColour                 = displayTextColour,
                BackgroundColour    = displayBackgroundColour,
                FontSize                     = displayfontSize
            };
            return PartialView("~/Views/Partials/pvEnumColourExample.cshtml",model);
        }
    }
}

View Model

namespace Web.Model.ColourPicker
{
    public class ColourPickerViewModel
    {
        public string BackgroundColour  { get; set; }
        public string TextColour        { get; set; }
        public string FontSize          { get; set; }
    }
}

Helper

As you can see, the helper class does almost all the work as it contains enum values. As you cannot use numbers in an enum, you can add a description and then use an extension to return the value if you need to do this. I have added the extension on how to achieve this.

using System;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
namespace Web.Helper.EnumHelper
{
    public static class EnumFontSize
    {
        
        public enum FontSizeEnum
        {
            [Description("twelvePx")]
            TwelvePx    = 12,
            [Description("fourteenPx")]
            FourteenPx  = 14,
            [Description("sixteenPx")]
            SixteenPx   = 16,
            [Description("twentyPx")]
            TwentyPx    = 20,
            // [Description("50")] 
            // 50 = 50 //This will not work, if you want to use a numbers use the extension below
        }
    }
    public static class DisplayFontSize
    {
        private static string DisplayFontDescription(this Enum value)
        {
            return ((DescriptionAttribute)Attribute.GetCustomAttribute(
                       value.GetType().GetFields(BindingFlags.Public | BindingFlags.Static)
                           .Single(x => x.GetValue(null).Equals(value)), typeof(DescriptionAttribute)))?.Description ??
                   value.ToString();
        }
        public static string SectionFontSize(int enumFontSize)
        {  
            string displayFontSize = "twelvePx";
            switch (enumFontSize)
            {
                case (int)EnumFontSize.FontSizeEnum.TwelvePx:
                    displayFontSize = EnumFontSize.FontSizeEnum.TwelvePx.DisplayFontDescription();
                    break;
                case (int)EnumFontSize.FontSizeEnum.FourteenPx:
                    displayFontSize = EnumFontSize.FontSizeEnum.FourteenPx.DisplayFontDescription();
                    break;
                case (int)EnumFontSize.FontSizeEnum.SixteenPx:
                    displayFontSize = EnumFontSize.FontSizeEnum.SixteenPx.DisplayFontDescription();
                    break;
                case (int)EnumFontSize.FontSizeEnum.TwentyPx:
                    displayFontSize = EnumFontSize.FontSizeEnum.TwentyPx.DisplayFontDescription();
                    break;
            }
            return displayFontSize;
        }
    }
    public static class EnumColourPicker
    {
        public enum ColourPickerEnum
        {
            Red,
            Black,
            Tomato,
            DarkOrange
        }
    }
}

Partial View

@inherits UmbracoViewPage<Web.Model.ColourPicker.ColourPickerViewModel>
<section class="@Model.BackgroundColour">
    <h1 class="@Model.TextColour @Model.FontSize">Text Colour</h1>
</section>