using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace TypeClass
{
public class TypeParse
{
/// <summary>
/// Determine whether the object is Int32 Type number
/// </summary>
/// <param name="Expression"></param>
/// <returns></returns>
public static bool IsNumeric(object Expression)
{
if (Expression != null)
{
int intVal;
return int.TryParse(Expression.ToString(), out intVal);
}
return false;
}
public static bool IsDouble(object Expression)
{
if (Expression != null)
{
double doubleVal;
return double.TryParse(Expression.ToString(), out doubleVal);
}
return false;
}
/// <summary>
/// string Type can be converted into bool type
/// </summary>
/// <param name="strValue"> The string to be converted </param>
/// <param name="defValue"> The default value </param>
/// <returns> The transformed bool Type the results </returns>
public static bool StrToBool(object Expression, bool defValue)
{
if (Expression != null)
{
bool boolValue;
if (bool.TryParse(Expression.ToString(), out boolValue))
return boolValue;
else
return defValue;
}
return defValue;
}
/// <summary>
/// Converts the object to Int32 type
/// </summary>
/// <param name="strValue"> The string to be converted </param>
/// <param name="defValue"> The default value </param>
/// <returns> The transformed Int32 Type the results </returns>
public static int StrToInt(object Expression, int defValue)
{
if (Expression != null)
{
int intValue;
if (int.TryParse(Expression.ToString(), out intValue))
return intValue;
else
return defValue;
}
return defValue;
}
/// <summary>
/// string Type can be converted into float type
/// </summary>
/// <param name="strValue"> The string to be converted </param>
/// <param name="defValue"> The default value </param>
/// <returns> The transformed float Type the results </returns>
public static float StrToFloat(object strValue, float defValue)
{
if (strValue != null)
{
float floatValue;
if (float.TryParse(strValue.ToString(), out floatValue))
return floatValue;
else
return defValue;
}
return defValue;
}
/// <summary>
/// string Type can be converted into Decimal type
/// </summary>
/// <param name="strValue"> The string to be converted </param>
/// <param name="defValue"> The default value </param>
/// <returns> The transformed Decimal Type the results </returns>
public static Decimal StrToDecimal(object strValue, Decimal defValue)
{
if (strValue != null)
{
Decimal decimalValue;
if (Decimal.TryParse(strValue.ToString(), out decimalValue))
return Math.Round(decimalValue,2);
else
return defValue;
}
return defValue;
}
/// <summary>
/// string Type can be converted into datetime type
/// </summary>
/// <param name="strValue"> The string to be converted </param>
/// <param name="defValue"> The default value </param>
/// <returns> The transformed datetime Type the results </returns>
public static DateTime StrToDateTime(object strValue, DateTime defValue)
{
if (strValue != null)
{
DateTime DateTimeValue;
if (DateTime.TryParse(strValue.ToString(), out DateTimeValue))
return DateTimeValue;
else
return defValue;
}
return defValue;
}
/// <summary>
/// Determines the given array of strings (strNumber) Are all the data in
/// </summary>
/// <param name="strNumber"> An array of strings to verify </param>
/// <returns> Is the back to add true Return if not false</returns>
public static bool IsNumericArray(string[] strNumber)
{
if (strNumber == null)
{
return false;
}
if (strNumber.Length < 1)
{
return false;
}
foreach (string id in strNumber)
{
if (!IsNumeric(id))
{
return false;
}
}
return true;
}
}
}