working on it ...

Filters

Explore Public Snippets

Sort by

Found 1,892 snippets matching: enum

    public by lbottaro  332805  2  4  0

    C# enum iteration

    This c# code allows to iterate through all element of an enum
    public enum Suit
    {
            Spades,
            Hearts,
            Clubs,
            Diamonds
    }
    
    foreach (Suit suit in Enum.GetValues(typeof(Suit)))
    {
       Console.WriteLine(suit);
    }

    public by cghersi  202325  0  5  1

    Java enum definition

    This is the classic enum definition with a member and the static getter to parse from the internal code
    /**
     * This enumeration defines ....<br/>
     */
    public enum MyComponent {
    	Comp1 			(1),
    	API 			(2),
    	unknown			(0);
    	
    	private int code;
    
    	private MyComponent (int code){
    		this.code = code;
    	}
    	
    	/**
    	 * Retrieves the integer code related to this component
    	 */
    	public int toInt() {
    		return code;
    	}
    	
    	/**
    	 * Looks up for the enumeration value corresponding to the given code
    	 * @param code internal code of the enumeration
    	 * @return enumeration value corresponding to the given code; unknown if not found
    	 */
    	public static MyComponent get(int code){
    		for (MyComponent p : MyComponent.values()) {
    			if(p.toInt() == code)
    				return p;
    		}
    		return MyComponent.unknown;
    	}
    }

    public by sTiLL-iLL @ SniPitz-KND  3594  4  8  25

    Enumerator.js... behind every enumeration you'll find a good Iterator. I guess.... LoL

    the future is now!
    // slick as snot Enumerable type!!!! yeah!
    
    var Enum = {
          create: function (collection) {
            var index=0;
            return {
              next: function() {
                if (index < collection.length) {
                  return ({ value: collection[index++], done: false });
                }
                else {
                  return ({ value: null, done: true});
                }
             }
          };
       }
    };
    
    // use it like this... (strings, bool, numbers, all gewd!)
    
    var arr=['rtJKvE', '!!@wrh', 433, 'yippe', 216,'PRdUrg', 3038, 'whaa?'],
      it = Enumerator.create(arr),
      primer = 0;
    
    while(primer !== null) {
      primer = it.next().value;
    	document.writeLine(primer);
    }
                

    public by lucky80  14901  105  6  0

    C#: Get Enum from Description attribute

    This is a generic extension method which gets the Description attribute from an Enum
    public static T GetValueFromDescription<T>(string description)
            {
                var type = typeof(T);
                if (!type.IsEnum) throw new InvalidOperationException();
                foreach (var field in type.GetFields())
                {
                    var attribute = Attribute.GetCustomAttribute(field,
                        typeof(DescriptionAttribute)) as DescriptionAttribute;
                    if (attribute != null)
                    {
                        if (attribute.Description == description)
                            return (T)field.GetValue(null);
                    }
                    else
                    {
                        if (field.Name == description)
                            return (T)field.GetValue(null);
                    }
                }
                throw new ArgumentException("Not found.", "description");
                // or return default(T);
            }

    public by lucky80  6463  2  6  1

    C#: Get Enum description attribute

    public static string GetEnumDescription<T>(T value)
            {
                FieldInfo fi = value.GetType().GetField(value.ToString());
    
                DescriptionAttribute[] attributes =
                    (DescriptionAttribute[])fi.GetCustomAttributes(
                        typeof(DescriptionAttribute),
                        false
                );
    
                if (attributes != null &&
                    attributes.Length > 0)
                {
                    return attributes[0].Description;
                }
                else
                {
                    return value.ToString();
                }
            }

    public by lucky80  2598  1  5  0

    C#: Get array string attributes of Enum object

    public static string[] GetEnumDescriptionArray<T>()
            {
                Array enumValue = System.Enum.GetValues(typeof(T));
                string[] result = new string[enumValue.Length];
                Int32 count = 0;
                foreach (var item in enumValue)
                {
                    FieldInfo fi = item.GetType().GetField(item.ToString());
    
                    DescriptionAttribute[] attributes =
                        (DescriptionAttribute[])fi.GetCustomAttributes(
                            typeof(DescriptionAttribute),
                            false
                    );
    
                    if (attributes != null &&
                        attributes.Length > 0)
                    {
                        result[count] = attributes[0].Description;
                    }
                    else
                    {
                        //return new string[0];
                        return null;
                    }
                    count++;
                }
                return result;
            }

    public by lbottaro  1994  2  5  0

    Java enum example

    Enum class with private constructor and toString() as overridden method
    public enum MyEnum {
    	
    	Enum_enable("enable"), 
    	Enum_test("test");
    
    	private final String enumName;
    
    	private MyEnum(final String name) {
    		this.enumName = name;
    	}
    
    	@Override
    	public String toString() {
    		return this.enumName;
    	}
    
    }

    public by cghersi  2094  0  6  0

    Enum as a configuration structure

    This structure for the enumeration let the developer specify some properties for each element of the enumeration. In this way, the enumeration becomes a container for a set of profiles to be used in the application. E.g. it is possible to specify a code (e.g. for serialization purpose) and then to specify other characteristics related to the same
    public enum ErrorCodes {
    	OK	(0),
    	FAIL	(-1);
        
    	private final int code;
        
    	ErrorCodes(int code) {
    		this.code = code;
    	}
        
    	/**
    	 * Retrieves the integer code related to this component
    	 */
    	public int toInt() {
    		return code;
    	}
    	
    	/**
    	 * Looks up for the enumeration value corresponding to the given code
    	 * @param code internal code of the enumeration
    	 * @return enumeration value corresponding to the given code; null if not found
    	 */
    	public static ErrorCodes get(int code){
    		for (ErrorCodes p : ErrorCodes.values()) {
    			if(p.toInt() == code)
    				return p;
    		}
    		return null;
    	}
    }

    public by passh  3492  15  4  0

    doctrine:mapping:convert type [ enum , set, varbinary, etc ] problem solved

    doctrine fails to load mysql tables if they have enum values or tables with no primary keys , this is the code solves it
    # Doctrine Configuration
    doctrine:
        dbal:
            driver:   pdo_mysql
            host:     "%database_host%"
            port:     "%database_port%"
            dbname:   "%database_name%"
            user:     "%database_user%"
            password: "%database_password%"
            charset:  UTF8
            # if using pdo_sqlite as your database driver:
            #   1. add the path in parameters.yml
            #     e.g. database_path: "%kernel.root_dir%/data/data.db3"
            #   2. Uncomment database_path in parameters.yml.dist
            #   3. Uncomment next line:
            #     path:     "%database_path%"
            schema_filter: ~^(?!hesk_active_sessions|hesk_tags|hesk_tickets_tags)~ 
            
            mapping_types:
             enum: string
             set: string
             varbinary: string
             tinyblob: text
                                           

    public by yicone  3225  0  4  0

    enum value map to string

    enum value map to string: EnumExtensions.cs
    /// <summary>
    /// http://stackoverflow.com/questions/2787506/cast-string-to-enum-with-enum-attribute
    /// todo: http://www.cnblogs.com/smalldust/archive/2006/04/25/384657.html 利用缓存优化
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static string GetDescription(this Enum value)
    {
        if (value == null)
        {
            throw new ArgumentNullException("value");
        }
    
        string description = value.ToString();
        FieldInfo fieldInfo = value.GetType().GetField(description);
        DescriptionAttribute[] attributes =
           (DescriptionAttribute[])
         fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
    
        if (attributes != null && attributes.Length > 0)
        {
            description = attributes[0].Description;
        }
        return description;
    }
    
    
    
    • Public Snippets
    • Channels Snippets