locked
Enums RRS feed

  • Question

  • Hi everyone :):):)

    I have another little question Lets Say I have

    public enum Group : int

        {

            Hour=24, Day=31 ,Week=7, Month=12 ,Year=1

        }

     

    and I have ComboBox box

     

    Sometimes I need to papulate it with all enum values except Year , sometimes I need to papulate it with all values except Week  (as strings )

     

    How can I do it easily because in a hard way I have accomplished it

     

    Thank you in advanced Anatoliy.

    Friday, May 20, 2011 8:45 AM

Answers

  • Hi again,

    Here you have a sample code:

    public enum Group : int
    {
    	Hour=24, Day=31 ,Week=7, Month=12 ,Year=1
    }
    
    void Main()
    {
    	Console.Write("All Groups:");
    	
    	foreach(var value in Enum.GetValues(typeof(Group)).Cast<Group>())
    	{
    		Console.Write(value);
    	}
    	
    	var groups = Enum.GetValues(typeof(Group)).Cast<Group>();
    	var myGroups = groups.Except(new List<Group> { Group.Day });	
    	
    	Console.Write("My Groups:");
    	
    	foreach(var value in myGroups)
    	{
    		Console.Write(value);
    	}	
    }
    

    Best regards,

    JA Reyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solucion de esta pregunta te ha sido útil.
    • Proposed as answer by Tim Tang Friday, May 20, 2011 11:35 AM
    • Marked as answer by Jackie-Sun Tuesday, May 24, 2011 8:29 AM
    Friday, May 20, 2011 11:12 AM
  • Hi Ja Reyes

    Thank you very much ,You   saved me

     

     

    Best Regards

    Anatoliy.

    • Marked as answer by Anatoliy50 Friday, May 20, 2011 12:04 PM
    Friday, May 20, 2011 12:04 PM
  • Hi,

    I'm glad.

    Please, mark as answer the specific response that helped you.

    Regards,

    JA Reyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solucion de esta pregunta te ha sido útil.
    • Marked as answer by Anatoliy50 Sunday, May 22, 2011 9:04 AM
    Friday, May 20, 2011 12:18 PM

All replies

  • Hi,

    You could perform an extenssion over Enums and iterate over the values:

    http://damieng.com/blog/2008/04/10/using-linq-to-foreach-over-an-enum-in-c/

    In this way, you can include a parameter saying which enum values you must avoid.

    Another option could be to use Except method:

    http://msdn.microsoft.com/en-us/vcsharp/aa336761.aspx#except1

    Regards,

    JA Reyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solucion de esta pregunta te ha sido útil.
    Friday, May 20, 2011 10:01 AM
  • Thanks for the links

     

    However I am afraid I  am a begginer  Those examples were to hard for me can you please write me  a simple code hear please

     

    Thank you in advance Anatoliy.

    Friday, May 20, 2011 11:00 AM
  • Hi again,

    Here you have a sample code:

    public enum Group : int
    {
    	Hour=24, Day=31 ,Week=7, Month=12 ,Year=1
    }
    
    void Main()
    {
    	Console.Write("All Groups:");
    	
    	foreach(var value in Enum.GetValues(typeof(Group)).Cast<Group>())
    	{
    		Console.Write(value);
    	}
    	
    	var groups = Enum.GetValues(typeof(Group)).Cast<Group>();
    	var myGroups = groups.Except(new List<Group> { Group.Day });	
    	
    	Console.Write("My Groups:");
    	
    	foreach(var value in myGroups)
    	{
    		Console.Write(value);
    	}	
    }
    

    Best regards,

    JA Reyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solucion de esta pregunta te ha sido útil.
    • Proposed as answer by Tim Tang Friday, May 20, 2011 11:35 AM
    • Marked as answer by Jackie-Sun Tuesday, May 24, 2011 8:29 AM
    Friday, May 20, 2011 11:12 AM
  • Hi Ja Reyes

    Thank you very much ,You   saved me

     

     

    Best Regards

    Anatoliy.

    • Marked as answer by Anatoliy50 Friday, May 20, 2011 12:04 PM
    Friday, May 20, 2011 12:04 PM
  • Hi,

    I'm glad.

    Please, mark as answer the specific response that helped you.

    Regards,

    JA Reyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solucion de esta pregunta te ha sido útil.
    • Marked as answer by Anatoliy50 Sunday, May 22, 2011 9:04 AM
    Friday, May 20, 2011 12:18 PM