C# NullSafe to prevent null reference exception



        #region NullSafeFunction
        ////****************************************************************
        //// NullSafeString
        ////****************************************************************
        public static string NullSafeString(object arg, string returnIfEmpty = Constants.General.EmptyString)
        {
            string returnValue = null;

            if ((object.ReferenceEquals(arg, DBNull.Value)) || (arg == null) || (object.ReferenceEquals(arg, string.Empty)))
            {
                returnValue = returnIfEmpty;
            }
            else
            {
                try
                {
                    returnValue = Convert.ToString(arg).Trim();
                    if (returnValue == "null")
                    {
                        returnValue = String.Empty;
                    }
                }
                catch
                {
                    returnValue = returnIfEmpty;
                }

            }

            return returnValue;

        }

        ////****************************************************************
        //// NullSafeShort
        ////****************************************************************
        public static Int16 NullSafeShort(object arg, Int16 returnIfEmpty = 0)
        {
            Int16 returnValue = 0;

            if ((object.ReferenceEquals(arg, DBNull.Value)) || (arg == null) || (object.ReferenceEquals(arg, string.Empty)))
            {
                returnValue = returnIfEmpty;
            }
            else
            {
                try
                {
                    returnValue = Convert.ToInt16(arg);
                }
                catch
                {
                    returnValue = returnIfEmpty;
                }
            }

            return returnValue;
        }

        ////****************************************************************
        //// NullSafeByte
        ////****************************************************************
        public static Byte NullSafeByte(object arg, Byte returnIfEmpty = 0)
        {
            Byte returnValue = 0;

            if ((object.ReferenceEquals(arg, DBNull.Value)) || (arg == null) || (object.ReferenceEquals(arg, string.Empty)))
            {
                returnValue = returnIfEmpty;
            }
            else
            {
                try
                {
                    returnValue = Convert.ToByte(arg);
                }
                catch
                {
                    returnValue = returnIfEmpty;
                }
            }

            return returnValue;
        }

        ////****************************************************************
        //// NullSafeInteger
        ////****************************************************************
        public static int NullSafeInteger(object arg, int returnIfEmpty = 0)
        {
            int returnValue = 0;

            if ((object.ReferenceEquals(arg, DBNull.Value)) || (arg == null) || (object.ReferenceEquals(arg, string.Empty)))
            {
                returnValue = returnIfEmpty;
            }
            else
            {
                try
                {
                    returnValue = Convert.ToInt32(arg);
                }
                catch
                {
                    returnValue = returnIfEmpty;
                }
            }

            return returnValue;
        }

        ////****************************************************************
        //// NullSafeInteger
        ////****************************************************************
        public static Int64 NullSafeInteger64(object arg, int returnIfEmpty = 0)
        {
            Int64 returnValue = 0;

            if ((object.ReferenceEquals(arg, DBNull.Value)) || (arg == null) || (object.ReferenceEquals(arg, string.Empty)))
            {
                returnValue = returnIfEmpty;
            }
            else
            {
                try
                {
                    returnValue = Convert.ToInt64(arg);
                }
                catch
                {
                    returnValue = returnIfEmpty;
                }
            }

            return returnValue;
        }

        ////****************************************************************
        ////   NullSafeDouble
        ////****************************************************************
        public static double NullSafeDouble(object arg, int returnIfEmpty = 0)
        {
            double returnValue = 0;

            if ((object.ReferenceEquals(arg, DBNull.Value)) || (arg == null) || (object.ReferenceEquals(arg, string.Empty)))
            {
                returnValue = returnIfEmpty;
            }
            else
            {
                try
                {
                    returnValue = Convert.ToDouble(arg);
                }
                catch
                {
                    returnValue = returnIfEmpty;
                }
            }

            return returnValue;
        }

        ////****************************************************************
        ////   NullSafeDecimal
        ////****************************************************************
        public static Decimal NullSafeDecimal(object arg, int returnIfEmpty = 0)
        {
            Decimal returnValue = 0;

            if ((object.ReferenceEquals(arg, DBNull.Value)) || (arg == null) || (object.ReferenceEquals(arg, string.Empty)))
            {
                returnValue = returnIfEmpty;
            }
            else
            {
                try
                {
                    returnValue = Convert.ToDecimal(arg);
                }
                catch
                {
                    returnValue = returnIfEmpty;
                }
            }

            return returnValue;
        }

        ////****************************************************************
        ////   NullSafeLong
        ////****************************************************************
        public static long NullSafeLong(object arg, long returnIfEmpty = 0)
        {
            long returnValue = 0;

            if ((object.ReferenceEquals(arg, DBNull.Value)) || (arg == null) || (object.ReferenceEquals(arg, string.Empty)))
            {
                returnValue = returnIfEmpty;
            }
            else
            {
                try
                {
                    returnValue = Convert.ToInt64(arg);
                }
                catch
                {
                    returnValue = returnIfEmpty;
                }
            }

            return returnValue;
        }


        //NULL SAFE FUNCTIONS
        public static T SafeConvert<T>(object s, T defaultValue)
        {
            try
            {
                if (string.IsNullOrEmpty(s.ToString()))
                    return defaultValue;
                return (T)Convert.ChangeType(s, typeof(T));
            }
            catch (Exception ex)
            {
                return defaultValue;
            }
        }

        //****************************************************************
        // NullSafeBoolean
        //****************************************************************
        public static bool NullSafeBoolean(object arg)
        {
            bool returnValue = false;

            if ((object.ReferenceEquals(arg, DBNull.Value)) || (arg == null) || (object.ReferenceEquals(arg, string.Empty)))
            {
                returnValue = false;
            }
            else
            {
                try
                {
                    returnValue = Convert.ToBoolean(arg);
                }
                catch
                {
                    returnValue = false;
                }
            }

            return returnValue;

        }

        public static DateTime NullSafeDateTime(object arg)
        {
            DateTime returnValue;

            try
            {
                DateTime.TryParse(arg.ToString(), out returnValue);
            }
            catch
            {
                returnValue = DateTime.MinValue;
            }

            return returnValue;
        }

        public static DateTime NullSafeDate(object arg)
        {
            DateTime returnValue = Constants.Dates.NULL_DATE;

            if ((object.ReferenceEquals(arg, DBNull.Value)) || (arg == null) || (object.ReferenceEquals(arg, string.Empty)))
            {
                returnValue = Constants.Dates.NULL_DATE;
            }
            else
            {
                try
                {
                    returnValue = Convert.ToDateTime(arg);
                    if (returnValue < Constants.Dates.NULL_DATE)
                    {
                        returnValue = Constants.Dates.NULL_DATE;
                    }
                }
                catch
                {
                    returnValue = Constants.Dates.NULL_DATE;
                }
            }

            return returnValue;
        }

        #endregion


Comments

Popular posts from this blog

قد رآی عینُ الله و ضرب یدُ الله

کیا عمران خان کی ماں قادیانی تھی ۔۔۔؟؟؟

اگر فاطمہ چوری کرتی تو کیا اس کا ہاتھ کاٹا جاتا؟ کیا یہ ممکن ہے؟ ("Agar (Meri Beti) Fatima (RaziAllahu Anha) Ne Bhi Chori Ki Hoti Tou Mai Uska Bhi Haath Kaat Leta”.)