Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the hueman domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/CloudIngenium.com/htdocs/wp-includes/functions.php on line 6114
How to: Convert a string into an Enum in C#.Net – Knowledge eXchange

How to: Convert a string into an Enum in C#.Net

Recently I came across the need to convert a string stored in the database into an enumeration in our application. I wasn’t really excited about having to write a switch statement nor having to maintain that code. Fortunately there is a command in .Net that allows you to parse that string into any enumeration which is a great help! Below is a sample of how to use Enum.Parse():

        /// <summary>
        /// This procedure generates the Permissions Mask necessary so that the correct permissions are applied by combining all the Masks that belong to a Security Permission Level
        /// </summary>
        /// <param name=”permissionLevel”>The Permission level for which the mask needs to be calculated</param>
        /// <returns></returns>
        private static Microsoft.SharePoint.SPBasePermissions GeneratePermissionMask(Security___Permission_Levels permissionLevel)
        {
            Microsoft.SharePoint.SPBasePermissions permissionMask = Microsoft.SharePoint.SPBasePermissions.EmptyMask;
            Type SPBasePermissionType = typeof(Microsoft.SharePoint.SPBasePermissions);
            foreach (Model.Security___Permissions_definition individualPermissionValue in permissionLevel.Security___Permissions_definition)
            {
                permissionMask |= (Microsoft.SharePoint.SPBasePermissions)Enum.Parse(SPBasePermissionType, individualPermissionValue.Security_Mask, true);
            }
            return (Microsoft.SharePoint.SPBasePermissions)permissionLevel.Permissions;
        }

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.