Esempio n. 1
0
        /// <summary>
        /// First tries to convert num to an Int64. Then to an Int32. If num64 is too large for an Int32 then it takes the right most digits that will put it in range.
        /// If it failes 0 is returned.
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static Int32 SafeInt32_Right(Object obj, String logDescription)
        {
            //Notes:
            //Int32.MaxValue = 2,147,483,647
            //Int32.MinValue = -2,147,483,648

            Int64 num64 = SafeConvert.SafeInt64(obj);
            Int32 num32 = SafeConvert.SafeInt32(obj);

            if (!((Int32.MinValue <= num64) && (num64 <= Int32.MaxValue)) || num64 == 0)
            {
                string strNum = obj.ToString().Right(10);
                num32 = SafeConvert.SafeInt32(strNum);
                if (!((Int32.MinValue <= num32) && (num32 <= Int32.MaxValue)) || num32 == 0)
                {
                    strNum = obj.ToString().Right(9);//If still out of range then take right most 9 digits ( guaranteed to be in range ).
                    num32  = SafeConvert.SafeInt32(strNum);
                }
                //This ends up making too many log entries.
                //string strMsg = MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodInfo.GetCurrentMethod().Name + " - [" + logDescription + "] " + obj.ToString() + " converted to " + num32 + ".";
                //LogHelper.AddLog("", "", strMsg);
            }

            return(num32);//num32 should now be within range for Int32.
        }
Esempio n. 2
0
        /// <summary>
        /// First tries to convert num to an Int32. Then to an Int16. If num32 is too large for an Int16 then it takes the right most digits that will put it in range.
        /// If it failes 0 is returned.
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static Int16 SafeInt16_Right(Object obj, String logDescription)
        {
            //Notes
            //Int16.MaxValue = 32767
            //Int16.MinValue = -32768

            Int32 num32 = SafeConvert.SafeInt32(obj);
            Int16 num16 = SafeConvert.SafeInt16(obj);

            if (obj != null && (!((Int16.MinValue <= num32) && (num32 <= Int16.MaxValue)) || num32 == 0))
            {
                string strNum = obj.ToString().Right(5);//Take right most 5 digits
                num16 = SafeConvert.SafeInt16(strNum);
                if (!((Int16.MinValue <= num16) && (num16 <= Int16.MaxValue)) || num16 == 0)
                {
                    strNum = obj.ToString().Right(4);//If still out of range then take right most 4 digits ( guaranteed to be in range.
                    num16  = SafeConvert.SafeInt16(strNum);
                }
                //This ends up making too many log entries.
                //string strMsg = MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodInfo.GetCurrentMethod().Name + " - [" + logDescription + "] " + obj.ToString() + " converted to " + num32 + ".";
                //LogHelper.AddLog("", "", strMsg);
            }
            return(num16);//num32 should now be within range for Int16.
        }