Beispiel #1
0
        internal object ParseInputString(ref string sInput)
        {
            object obj = (object)sInput;

            if (sInput[0] == '#' && sInput.Length != 1)
            {
                sInput = sInput.Substring(1, checked (sInput.Length - 2));
                if (Operators.CompareString(sInput, "NULL", false) == 0)
                {
                    obj = (object)DBNull.Value;
                }
                else if (Operators.CompareString(sInput, "TRUE", false) == 0)
                {
                    obj = (object)true;
                }
                else if (Operators.CompareString(sInput, "FALSE", false) == 0)
                {
                    obj = (object)false;
                }
                else if (Operators.CompareString(Strings.Left(sInput, 6), "ERROR ", false) == 0)
                {
                    int num = 0;
                    if (sInput.Length > 6)
                    {
                        num = IntegerType.FromString(Strings.Mid(sInput, 7));
                    }
                    obj = (object)num;
                }
                else
                {
                    try
                    {
                        obj = (object)DateTime.Parse(Utils.ToHalfwidthNumbers(sInput, Utils.GetCultureInfo()));
                    }
                    catch (StackOverflowException ex)
                    {
                        throw ex;
                    }
                    catch (OutOfMemoryException ex)
                    {
                        throw ex;
                    }
                    catch (ThreadAbortException ex)
                    {
                        throw ex;
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
            return(obj);
        }
Beispiel #2
0
        /// <summary>Returns an <see langword="Integer" /> value that corresponds to the specified object. </summary>
        /// <param name="Value">Required. Object to convert to an <see langword="Integer" /> value.</param>
        /// <returns>The <see langword="Integer" /> value that corresponds to <paramref name="Value" />.</returns>
        public static int FromObject(object Value)
        {
            if (Value == null)
            {
                return(0);
            }
            IConvertible ValueInterface = Value as IConvertible;

            if (ValueInterface != null)
            {
                switch (ValueInterface.GetTypeCode())
                {
                case TypeCode.Boolean:
                    return(-(ValueInterface.ToBoolean((IFormatProvider)null) ? 1 : 0));

                case TypeCode.Byte:
                    if (Value is byte)
                    {
                        return((int)(byte)Value);
                    }
                    return((int)ValueInterface.ToByte((IFormatProvider)null));

                case TypeCode.Int16:
                    if (Value is short)
                    {
                        return((int)(short)Value);
                    }
                    return((int)ValueInterface.ToInt16((IFormatProvider)null));

                case TypeCode.Int32:
                    if (Value is int)
                    {
                        return((int)Value);
                    }
                    return(ValueInterface.ToInt32((IFormatProvider)null));

                case TypeCode.Int64:
                    if (Value is long)
                    {
                        return(checked ((int)(long)Value));
                    }
                    return(checked ((int)ValueInterface.ToInt64((IFormatProvider)null)));

                case TypeCode.Single:
                    if (Value is float)
                    {
                        return(checked ((int)Math.Round((double)(float)Value)));
                    }
                    return(checked ((int)Math.Round((double)ValueInterface.ToSingle((IFormatProvider)null))));

                case TypeCode.Double:
                    if (Value is double)
                    {
                        return(checked ((int)Math.Round((double)Value)));
                    }
                    return(checked ((int)Math.Round(ValueInterface.ToDouble((IFormatProvider)null))));

                case TypeCode.Decimal:
                    return(IntegerType.DecimalToInteger(ValueInterface));

                case TypeCode.String:
                    return(IntegerType.FromString(ValueInterface.ToString((IFormatProvider)null)));
                }
            }
            throw new InvalidCastException(Utils.GetResourceString("InvalidCast_FromTo", Utils.VBFriendlyName(Value), "Integer"));
        }