Full-Stack freelance Umbraco developer in the UK.

George Phillipson - Freelance Umbraco developer

Using enum in a switch case

Man pointing at C#
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<ColorPickerValueConverter.PickedColor>("textColour").Label;
            string backgroundColour = umbContent.GetPropertyValue<ColorPickerValueConverter.PickedColor>("sectionBackgroundColour").Label;
            int fontSize = umbContent.GetPropertyValue<int>("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);
        }
    }
}
namespace Web.Model.ColourPicker
{
    public class ColourPickerViewModel
    {
        public string BackgroundColour  { get; set; }
        public string TextColour        { get; set; }
        public string FontSize          { get; set; }
    }
}
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
        }
    }
}
@inherits UmbracoViewPage<Web.Model.ColourPicker.ColourPickerViewModel>

<section class="@Model.BackgroundColour">
    <h1 class="@Model.TextColour @Model.FontSize">Text Colour</h1>
</section>

Please enter your comment.