Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
S

Simon Allaeys

@Simon Allaeys
About
Posts
2
Topics
0
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • Enumerators
    S Simon Allaeys

    While using these methods I found a shortcoming... there is no support for flags! So this is how I improved it:

    public static bool TryParseEnum(object value, out EnumType result)
    {
    result = default(EnumType);
    if (value == null) return false;
    var type = typeof (EnumType);
    if (!type.IsEnum) return false;
    int iValue;
    return Int32.TryParse(value.ToString(), out iValue)
    ? TryParseEnum(iValue, out result)
    : TryParseEnum(value.ToString(), out result);
    }

    public static bool TryParseEnum(int value, out EnumType result)
    {
    result = default(EnumType);
    var type = typeof(EnumType);
    if (!type.IsEnum) return false;
    result = (EnumType)Enum.Parse(type, value.ToString());
    // Verify if the result is valid,
    // Enum.Parse might just return the input value, while this is not a defined value of the enum
    if (result.ToString().Contains(", ") || Enum.IsDefined(type, result))
    return true;
    result = default(EnumType);
    return false;
    }

    public static bool TryParseEnum(string value, out EnumType result)
    {
    result = default(EnumType);
    if (string.IsNullOrEmpty(value)) return false;
    var type = typeof(EnumType);
    if (!type.IsEnum) return false;
    value = value.ToUpperInvariant();
    try
    {
    result = (EnumType)Enum.Parse(type, value, true);
    return true;
    }
    catch (ArgumentException)
    {
    return false;
    }
    }

    :edited: Edited the object method to use the two other methods, this should be more reliable for both normal enums and bit flag enums. Casting/parsing values which are non-numeric and non-string values is anyway not possible in .NET.

    modified on Wednesday, February 10, 2010 6:31 AM

    Clever Code json help question

  • Enumerators
    S Simon Allaeys

    Why all the fuss about "starting programming a long time ago before the many of the rest of us"? I've been programming for 2 years now, and I wrote the folowing code snippit about one year ago:

    public static bool TryParseEnum<TEnum>(int value, out TEnum result)
    {
      result = default(TEnum);
      var type = typeof (TEnum);
      if (!type.IsEnum) return false;
      try
      {
        result = (TEnum) Enum.ToObject(type, value);
        return true;
      }
      catch
      {
        return false;
      }
    }
    
    public static bool TryParseEnum<TEnum>(string value, out TEnum result)
    {
      result = default(TEnum);
      var type = typeof(TEnum);
      if (!type.IsEnum) return false;
      value = value.ToUpperInvariant();
      var names = Enum.GetNames(type);
      // Enum.IsDefined() is only available as case sensitive
      foreach (var name in names)
      {
        if (name.ToUpperInvariant() != value)
          continue;
        result = (TEnum)Enum.Parse(type, value, true);
        return true;
      }
      return false;
    }
    

    Advantages of this code: - you know if parsing succeeded (while you can't know when returning default values) - case insensitive

    modified on Thursday, December 3, 2009 3:13 PM

    Clever Code json help question
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups