none
Enum - GetName RRS feed

  • Question

  • Hello,
    Works well

      

    it's all right with logging the enums.
       ....{item.State,45:f}");

    I still tested this function, it will not work, it's always null.
    Maybe I'll need it. Does anyone know why that is null?null why

    public static string GetName(Type enumType, object value);
    
    public string ConvertToString<T>(int enumValue)
    {
    	return Enum.GetName(typeof(T), enumValue);
    }
    
    string m = Enum.GetName(typeof(StatePosition), 3);
    Type type = sb.State.GetType();
    string name = Enum.GetName(type, sb.State);
    if (name != null)
    {
    	var field = type.GetField(name);
    	if (field != null)
    	{
    	}
    }
    With best regards Markus

    Wednesday, October 31, 2018 5:39 PM

Answers

  • Hi Markus Freitag,

    Enum.GetName retrieves the name of the constant in the specified enumeration that has the specified value. which does not support Flags, so that you could write custom Enum extension method to achieve your requirement.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, November 8, 2018 2:06 AM
    Moderator

All replies

  • Since sb.State currently is a combination of enumeration values (using ‘|’), Enum.GetName cannot find a single name that corresponds to sb.State.

    If you want all of the names that are specified in sb.State, try this:

       string [ ] names =  sb.State.ToString("F").Split ( new[] { ", " }, StringSplitOptions.None );

    or this:

       string [] names =
            Enum
                  .GetValues( typeof(StatePosition) )
                  .Cast<StatePosition>()
                  .Where( v => ( sb.State & v ) != 0 )
                  .Select( v => v.ToString() )
                  .ToArray();



    Wednesday, October 31, 2018 7:14 PM
  • Hello,

    Yes ok. 

    Enum.GetName(typeof(T), enumValue);

    But why it the return value here null?

    What is the right call for this function?

    Regards Markus

    Thursday, November 1, 2018 12:15 PM
  • Hi Markus Freitag,

    Could you pleas share a complete to reproduce the issue.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 2, 2018 5:50 AM
    Moderator
  • Hi Zhanglong Wu,
    here is the Code.

    public class Board
    {
    	public enum TypePosition
    	{
    		NotDefine = -1,
    		NotActive = 0,
    		Active = 1
    	}
    
    	[Flags]
    	public enum StatePosition
    	{
    		ProcessedNot = 0,
    		ProcessedAlready = 1,
    		ProcessedAndReadIt = 2,
    		ProcessedAndNotReadIt = 4,
    		ProcessedIsBad = 8,
    		Special = 16
    		//ProcessedAndReadIt = ProcessedAlready | ReadIt,
    		//ProcessedAndNotReadIt = ProcessedAlready | NotReadIt,
    		//ProcessedIsBad= ProcessedAlready | IsBad
    	}
        public StatePosition State { get; set; }
    }
    
    main.cs
    
    
    public void Test()
    {
     string stateBad = "BAD";
     string stateRead = "PASS";
     
     Board sb = new Board();
     
     Board.StatePosition currentStatePosition = Board.StatePosition.ProcessedAlready;
    
    if (stateBad == "BAD")
    	currentStatePosition = currentStatePosition | SingleBoard.StatePosition.ProcessedIsBadmark;
    
    if (stateRead == "PASS")
    	currentStatePosition = currentStatePosition | SingleBoard.StatePosition.ProcessedAndReadIt;
    }
    
    if ((sb.State & StatePosition.ProcessedAlready) == StatePosition.ProcessedAlready)
    {
    	// Already Done
    }
    
    
    string m = Enum.GetName(typeof(StatePosition), 3);
    
    
    Type type = sb.State.GetType();
    string name = Enum.GetName(type, sb.State);
    if (name != null)
    {
    	var field = type.GetField(name);
    	if (field != null)
    	{
    	}
    }

    Greeting Markus and thanks in advance.



    Friday, November 2, 2018 4:23 PM
  • Hi Markus Freitag,

    Base on your description, I create a simple console application and it works as expect. 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using static App3.Board;
    
    namespace App3
    {
        class Program
        {
    
    
            public string ConvertToString<T>(int enumValue)
            {
                return Enum.GetName(typeof(T), enumValue);
            }
            static void Main(string[] args)
            {
                //string stateBad = "BAD";
                //string stateRead = "PASS";
    
                Board sb = new Board();
    
                //Board.StatePosition currentStatePosition = Board.StatePosition.ProcessedAlready;
                //if (stateBad == "BAD")
                //    currentStatePosition = currentStatePosition | SingleBoard.StatePosition.ProcessedIsBadmark;
    
                //if (stateRead == "PASS")
                //    currentStatePosition = currentStatePosition | SingleBoard.StatePosition.ProcessedAndReadIt;
                //if ((sb.State & StatePosition.ProcessedAlready) == StatePosition.ProcessedAlready)
                //{
                //    // Already Done
                //}
    
                Type type2 = typeof(StatePosition);
    
    
                string m = Enum.GetName(type2, 4);
    
    
                Type type = sb.State.GetType();
                string name = Enum.GetName(type, sb.State);
                if (name != null)
                {
                    var field = type.GetField(name);
                    if (field != null)
                    {
                    }
                }
            }
    
        }
    
        public class Board
        {
            public enum TypePosition
            {
                NotDefine = -1,
                NotActive = 0,
                Active = 1
            }
    
            [Flags]
            public enum StatePosition
            {
                ProcessedNot = 0,
                ProcessedAlready = 1,
                ProcessedAndReadIt = 2,
                ProcessedAndNotReadIt = 4,
                ProcessedIsBad = 8,
                Special = 16
                //ProcessedAndReadIt = ProcessedAlready | ReadIt,
                //ProcessedAndNotReadIt = ProcessedAlready | NotReadIt,
                //ProcessedIsBad= ProcessedAlready | IsBad
            }
            public StatePosition State { get; set; }
        }
    }
    

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 5, 2018 3:29 AM
    Moderator
  • Hi Zhanglong Wu,
    Now I know the reason.
    Is working well with unique ID
    I expected that.

    7 set all flags names,but I think it is not possible, right?Reason

    using System;
    using System.Windows;
    
    namespace WpfApp1
    {
    
        public class Board
        {
            public enum TypePosition
            {
                NotDefine = -1,
                NotActive = 0,
                Active = 1
            }
    
            [Flags]
            public enum StatePosition
            {
                ProcessedNot = 0,
                ProcessedAlready = 1,
                ProcessedAndReadIt = 2,
                ProcessedAndNotReadIt = 4,
                ProcessedIsBad = 8
            }
            public StatePosition State { get; set; }
        }
    
    
        /// <summary>
        /// Interaktionslogik für MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public string ConvertToString<T>(int enumValue)
            {
                return Enum.GetName(typeof(T), enumValue);
            }
    
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Test(object sender, RoutedEventArgs e)
            {
                Type type2 = typeof(Board.StatePosition);
    
                Board sb = new Board();
                string m = Enum.GetName(type2, 4);
    
                string moreThanOne = Enum.GetName(type2, 7);   // ### here is the problem!!
    
    
                Type type = sb.State.GetType();
                string name = Enum.GetName(type, sb.State);
                if (name != null)
                {
                    var field = type.GetField(name);
                    if (field != null)
                    {
                        // for what helpful?
                    }
                }
            }
        }
    }

    Greeting Markus

    Monday, November 5, 2018 6:02 PM
  • Hi Markus Freitag,

    >>string moreThanOne = Enum.GetName(type2, 7);   // ### here is the problem!!

    I could not find any item in StatePosition which value is 7. so that it will return null.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, November 7, 2018 5:22 AM
    Moderator
  • Hi Zhanglong,
    [Flags]
    public enum StatePosition
    {
     ProcessedNot = 0,
     ProcessedAlready = 1,
     ProcessedAndReadIt = 2,
     ProcessedAndNotReadIt = 4,
     ProcessedIsBad = 8
    }

    I used here flags.
    ProcessedAlready | ProcessedAndReadIt | ProcessedAndNotReadIt
    0001
    0010
    0100
    ----
    0111 -> 7
    So I excepted as name value ProcessedAlready | ProcessedAndReadIt | ProcessedAndNotReadIt
    But I think that is not possible.
    Do you understand my problem?
    Regards Markus

    Wednesday, November 7, 2018 5:09 PM
  • Hi Markus Freitag,

    Enum.GetName retrieves the name of the constant in the specified enumeration that has the specified value. which does not support Flags, so that you could write custom Enum extension method to achieve your requirement.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, November 8, 2018 2:06 AM
    Moderator