Link to home
Create AccountLog in
Avatar of solution1368
solution1368

asked on

c#, data type

1. stuntclaimpayoutamount when i try to put return null instead of return 0. it alerts me...
my logic is if nothing there, in stuntclaimpayoutamount it should be null/nothing.

2. same issue for productEquipmentReturnDate. I just want return null/nothing
when it is really nothing.

how can i fix it?

 public decimal stuntClaimPayoutAmount {
            get
            {
                if (!String.IsNullOrEmpty(StuntPayoutAmount.Text.Trim()))
                {
                    return decimal.Parse(StuntPayoutAmount.Text.Trim());
                }
                else
                {
                    return 0;
                }
            }
        }


   public DateTime productEquipmentReturnDate { get {
            if (!String.IsNullOrWhiteSpace(ProductEquipmentReturnDate.Text.Trim()))
            { return DateTime.Parse(ProductEquipmentReturnDate.Text.Trim()); }
            else
            {
            return DateTime.Now;
            }
        }
        }
Avatar of Kumaraswamy R
Kumaraswamy R
Flag of India image

public object stuntClaimPayoutAmount {
            get
            {
                if (!String.IsNullOrEmpty(StuntPayoutAmount.Text.Trim()))
                {
                    return decimal.Parse(StuntPayoutAmount.Text.Trim());
                }
                else
                {
                    return null;
                }
            }
        }
value assign place - check value is null then convert to decimal and assign

public Object productEquipmentReturnDate { get {
            if (!String.IsNullOrWhiteSpace(ProductEquipmentReturnDate.Text.Trim()))
            { return DateTime.Parse(ProductEquipmentReturnDate.Text.Trim()); }
            else
            {
            return null;
            }
        }
        }

value assign place - check value is null then convert to datetime and assign
You could use a Nullable data type, which can hold the normal values, plus null:
http://msdn.microsoft.com/en-us/library/vstudio/1t3y8s4s.aspx
Nullable is definitely more preferable than returning object    8 )
Howdy

Building on Idle_Mind suggestion I would also be utilising a nullable decimal, you may also want to look at how you handle your decimal parsing.

public decimal? stuntClaimPayoutAmount
{
    get
    {
        if (!String.IsNullOrEmpty(StuntPayoutAmount.Text.Trim()))
        {
            decimal returnValue;
            if (decimal.TryParse(StuntPayoutAmount.Text.Trim(), returnValue))
            {
                return returnValue;
            }

            // we were unable to parse the string as decimal 
            // you will need to handle this
            // decimal.parse would just throw an unhandled exception
            return null;
        }
        else
        {
            return null;
        }
    }
}

Open in new window


Hope that helps
Avatar of solution1368
solution1368

ASKER

james-ct16: Using decimal? is fine but like you say, how to handle the decimal parsing....

like

stuntClaimPayoutAmount = decimal.parse(textbox1.text.trim()); will be failed.
Howdy
You could if appropriate limit you textbox to only accepted decimal input. Consider tracking text changes or key presses and ignoring values that do not produce a valid decimal.

By using  decimal.TryParse opposed to decimal.parse no exception is thrown if the text value does not parse. TryParse returns a boolean value.

It may be appropriate simply to return null if the textbox input is not decimal.

Hope that helps
hope to see some sample codes...:-D Thanks
ASKER CERTIFIED SOLUTION
Avatar of james-ct16
james-ct16
Flag of Australia image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account