My Convertor type

Always returns default values. Never throws Exception or returns NULL with exception of ToDateTime(). Use this class to reuse code in replace of Non-Nullable types with exception of DateTime which returns Nullable DateTime.

For the detailed conversion of enum to string and string to enum, go here.

public sealed class Convertor
{
    private Convertor() { }

    #region byte[]
    /// <summary>
    /// Converts string into array of bytes
    /// </summary>
    /// <param name="strInput"></param>
    /// <returns></returns>
    public static byte[] ToByteArray(string strInput)
    {
        int intCounter; char[] arrChar;
        arrChar = strInput.ToCharArray();
        byte[] arrByte = new byte[arrChar.Length];
        for (intCounter = 0; intCounter <= arrByte.Length - 1; intCounter++)
            arrByte[intCounter] = Convert.ToByte(arrChar[intCounter]);

        return arrByte;
    }
    #endregion

    #region boolean
    /// <summary>
    /// Converts to Boolean.
    /// </summary>
    public static bool ToBoolean(string theValue)
    {
        if (theValue == null)
            return false;

        theValue = theValue.Trim();
        try
        {
            if (StringRegexValidator.IsInteger(theValue))
                return Convert.ToBoolean(Convertor.ToInteger(theValue));
            else
                return Convert.ToBoolean(theValue.ToLower());
        }
        catch { }

        return false;

    }
    /// <summary>
    /// Converts to Boolean.
    /// </summary>
    public static bool ToBoolean(object theValue)
    {
        if (theValue == null)
            return false;
        try
        {
            if (theValue is IConvertible)
                return Convert.ToBoolean(theValue);
        }
        catch { }

        return false;
    }
    #endregion

    #region string
    /// <summary>
    /// Converts to String. Returns empty string if cannot convert
    /// </summary>
    public static string ToString(bool theValue)
    {
        return Convert.ToString(theValue).ToLower();
    }
    /// <summary>
    /// Converts to String. Returns empty string if cannot convert
    /// </summary>
    public static string ToString(object theValue)
    {
        if (theValue != null)
        {
            if (theValue.GetType().Name == "DateTime")
                return ToString((DateTime)theValue);
            else
                return Convert.ToString(theValue.ToString());
        }
        else
            return "";
    }
    /// <summary>
    /// Converts any enum to string
    /// </summary>
    public static string ToString(Enum EnumValue)
    {
        if (EnumValue == null)
            throw new Exception("StringToEnum(): EnumValue is null");

        FieldInfo fi = EnumValue.GetType().GetField(EnumValue.ToString());
        DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
        if (attributes.Length > 0)
        {
            return attributes[0].Description;
        }
        else
        {
            return EnumValue.ToString();
        }
    }
    #endregion

    #region char
    /// <summary>
    /// Converts to Char. Returns ' ' if cannot convert
    /// </summary>
    public static char ToChar(object theValue)
    {
        if (theValue != null)
        {
            try
            {
                return Convert.ToChar(theValue);
            }
            catch { return ' '; }
        }
        else
            return ' ';
    }

    /// <summary>
    /// Converts to Char. Returns ' ' if cannot convert
    /// </summary>
    public static char ToChar(string theValue)
    {
        if (theValue != null)
        {
            try
            {
                return Convert.ToChar(theValue);
            }
            catch { return ' '; }
        }
        else
            return ' ';
    }
    #endregion

    #region long
    /// <summary>
    /// Converts to Long. Returns 0 if cannot convert
    /// </summary>
    public static long ToLong(string theValue)
    {
        long value = 0;
        long.TryParse(theValue, out value);
        return value;
    }

    /// <summary>
    /// Converts to Long. Returns 0 if cannot convert
    /// </summary>
    public static long ToLong(object theValue)
    {
        try
        {
            if (theValue is IConvertible)
                return Convert.ToInt64(theValue);
        }
        catch{ }
        return 0;
    }
    #endregion

    #region double
    /// <summary>
    /// Converts to Double. Returns 0.0 if cannot convert
    /// </summary>
    public static double ToDouble(string theValue)
    {
        double value = 0;
        double.TryParse(theValue, out value);
        return value;
    }

    /// <summary>
    /// Converts to Double. Returns 0.0 if cannot convert
    /// </summary>
    public static double ToDouble(object theValue)
    {
        try
        {
            if (theValue is IConvertible)
                return Convert.ToDouble(theValue);
        }
        catch{ }
        return 0;
    }
    #endregion

    #region datetime
    /// <summary>
    /// Converts to DateTime. Returns null if cannot convert
    /// </summary>
    public static DateTime? ToDateTime(object theValue)
    {
        try
        {
            if (theValue is IConvertible)
                return Convert.ToDateTime(theValue);
        }
        catch{ }
        return null;
    }

    /// <summary>
    /// Converts to DateTime. Returns null if cannot convert
    /// </summary>
    public static DateTime? ToDateTime(object theValue, IFormatProvider provider)
    {
        try
        {
            if (theValue is IConvertible)
                return Convert.ToDateTime(theValue, provider);
        }
        catch{ }
        return null;
    }


    /// <summary>
    /// Converts to DateTime. Returns null if cannot convert
    /// </summary>
    public static DateTime? ToDateTime(string theValue)
    {
        try
        {
            return Convert.ToDateTime(theValue);
        }
        catch{ }
        return null;
    }

    /// <summary>
    /// Converts to DateTime. Returns null if cannot convert
    /// </summary>
    public static DateTime? ToDateTime(string theValue, IFormatProvider provider)
    {
        try
        {
            return Convert.ToDateTime(theValue, provider);
        }
        catch { }
        return null;
    }
    #endregion

    #region int
    /// <summary>
    /// Converts to Integer. Returns 0 if cannot convert
    /// </summary>
    public static int[] ToInteger(string[] array)
    {
        try
        {
            if (array == null || array.Length == 0)
                return new int[] { };

            int[] values = new int[array.Length];

            for (int i = 0; i < array.Length; i++)
                values[i] = Convertor.ToInteger(array[i]);

            return values;
        }
        catch
        {
            return new int[] { };
        }
    }
    /// <summary>
    /// Converts to Integer. Returns 0 if cannot convert
    /// </summary>
    public static int ToInteger(string theValue)
    {
        if (theValue == null)
            return 0;

        theValue = theValue.Trim();

        if (theValue.Contains(".") || theValue.Contains(","))
        {
            double Value = ToDouble(theValue);
            return (int)(Value >= 0 ? Value + 0.5 : Value - 0.5);
        }
        else
        {
            int value = 0;
            int.TryParse(theValue, out value);
            return value;
        }
    }

    /// <summary>
    /// Converts to Integer. Returns 0 if cannot convert
    /// </summary>
    public static int ToInteger(object theValue)
    {
        try
        {
            if (theValue is IConvertible)
                return Convert.ToInt32(theValue);
        }
        catch{ }
        return 0;
    }

    /// <summary>
    /// Converts to Integer. Returns 0 if cannot convert
    /// </summary>
    public static int ToInteger(bool theValue)
    {
        if (theValue)
            return 1;
        else
            return 0;
    }

    /// <summary>
    /// Converts to Natural Number. Returns 1 if cannot convert
    /// </summary>
    public static int ToNaturalNumber(object theValue)
    {
        try
        {
            if (theValue is IConvertible)
            {
                int x = Convert.ToInt32(theValue);
                if (x <= 0)
                    x = 1;
                return x;
            }

        }
        catch { }
        return 1;
    }

    /// <summary>
    /// Converts to Natural Number. Returns 1 if cannot convert
    /// </summary>
    public static int ToNaturalNumber(string theValue)
    {
        int x = ToInteger(theValue);
        if (x <= 0)
            x = 1;
        return x;
    }

    /// <summary>
    /// Converts to Positive Integer. Returns 0 if cannot convert
    /// </summary>
    public static int ToPositiveInteger(object theValue)
    {
        try
        {
            if (theValue is IConvertible)
            {
                int x = Convert.ToInt32(theValue);
                if (x < 0)
                    x = 0;
                return x;
            }

        }
        catch{ }
        return 0;
    }

    /// <summary>
    /// Converts to Positive Integer. Returns 0 if cannot convert
    /// </summary>
    public static int ToPositiveInteger(string theValue)
    {
        int x = ToInteger(theValue);
        if (x <= 0)
            x = 0;
        return x;
    }
    #endregion

    #region enum
    /// <summary>
    /// Converts any string to the specified enum type, returns ArgumentException if not found.
    /// </summary>
    public static object ToEnum(string StringValue, Type EnumType)
    {
        if (EnumType == null)
            throw new Exception("StringToEnum(): EnumType is null");

        if (StringValue == null)
            throw new Exception("StringToEnum(): StringValue is null");

        string[] names = Enum.GetNames(EnumType);
        foreach (string name in names)
        {
            if (ToString((Enum)Enum.Parse(EnumType, name)).Equals(StringValue))
            {
                return Enum.Parse(EnumType, name);
            }
        }

        throw new ArgumentException("The StringValue is not a description or value of the specified EnumType.");
    }
    #endregion

    #region decimal
    public static decimal ToDecimal(object value)
    {
        if (value == null)
            return 0;

        decimal theValue = 0;
        try
        {
            if (value is IConvertible)
            {
                theValue = Convert.ToDecimal(value);
            }
        }
        catch{ }
        {
            theValue = 0;
        }
            
        return theValue;
    }

    public static decimal ToDecimal(string theValue)
    {
            if (theValue == null)
                return 0;

            theValue = theValue.Trim();

            theValue = theValue.Replace(",", ".");

            decimal value = 0;

            decimal.TryParse(theValue, out value);

            return value;
    }
    #endregion

}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s