"Return" in C#

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

LVL 8
CamilliaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

peprCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ste5anSenior DeveloperCommented:
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 DeveloperCommented:
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.
CamilliaAuthor Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.