"Return" in C#

Camillia
Camillia used Ask the Experts™
on
Please help me understand why C# throws this error when there's already a return statement there? I can add another return to fix it but why does it do that?

ee1.png
This is how I can fix it

public decimal CashBonusBalance
		{
			set {this.label_CashBonusBalance.Text = string.Format("{0:C}",value);}
            get
            {
                if (label_CashBonusBalance != null)
                    return decimal.Parse(label_CashBonusBalance.Text);
                return 0; //**** adding an extra return
            }
        }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
The return statement is missing in the case when the condition does not hold. A return statement must be there in all branches.

Imagine what would happen if label_CashBonusBalance would be null. What would you get from the property? So, the "extra return" is not that much extra. It is required in such case. Another fix could be to use the other return in the else branch. In my opinion, using it as you wrote is more readable and more robust.
ste5anSenior Developer
Commented:
Because a getter must return in every code path a value. The if statement is

if () { } else { }

Open in new window

and in your original case, this translates to

public decimal CashBonusBalance
{
    set {this.label_CashBonusBalance.Text = string.Format("{0:C}",value);}
    get
    {
        if (label_CashBonusBalance != null)
        {
            return decimal.Parse(label_CashBonusBalance.Text);
        }
        else
        {
        }
    }
}

Open in new window

Now we see, that there is a return missing. Either in the else path or at the end of the procedure. E.g.

public decimal CashBonusBalance
{
    set {this.label_CashBonusBalance.Text = string.Format("{0:C}",value);}
    get
    {
        if (label_CashBonusBalance != null)
        {
            return decimal.Parse(label_CashBonusBalance.Text);
        }
        else
        {
            return 0; // Default value.
        }
    }
}

// OR

public decimal CashBonusBalance
{
    set {this.label_CashBonusBalance.Text = string.Format("{0:C}",value);}
    get
    {
        if (label_CashBonusBalance != null)
        {
            return decimal.Parse(label_CashBonusBalance.Text);
        }

        return 0; // Default value.
    }
}

Open in new window

In C/C++ more complex code flows are possible, thus there is the rule of thumb of using a single point of exit:

public decimal CashBonusBalance
{
    set {this.label_CashBonusBalance.Text = string.Format("{0:C}",value);}
    get
    {
        decimal result = 0; // A meaningful defined default value.
        if (label_CashBonusBalance != null)
        {
            result = decimal.Parse(label_CashBonusBalance.Text);
        }

        return result;
    }
}

Open in new window


p.s. imho it is bad style to omit parentheses in the simple if case..

p.p.s. you're inconsistently accessing label_CashBonusBalance. And the test  label_CashBonusBalance != null for a given value makes no sense, cause it test whether the label exists.
Kyle AbrahamsSenior .Net Developer
Commented:
Your model is off.

the label should be binding to the value, you shouldn't update from the label.

With that said ste5an is correct  - the property must always return a value.
I understand it now. Thanks

Your model is off.
WinForm code from 2002. This is the least of the problems on this project. I'll fix it, tho.

Thanks for the help, as always.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial