Link to home
Start Free TrialLog in
Avatar of Camillia
CamilliaFlag for United States of America

asked on

Code reivew : Variable hides property?

I had a code review done for my app. This is the comment I got. Not sure what it means..
private int? Ethnic
    {
        get
        {
            int? Ethnic = null; // CODE REVIEW: This variable declaration hides Property, consider changing the name or removing the declaration.
            if (!string.IsNullOrEmpty(ddlEthnic.SelectedValue)) //not a required field
                Ethnic = int.Parse(ddlEthnic.SelectedValue);

            return Ethnic;
        }

           }

Open in new window

Avatar of edemcs
edemcs
Flag of United States of America image

You declared it as Private and therefore nothing can access it except for the class itself.
Oh, and you're redundantly creating a local variable with the same name as the property.  Give your local variable a different name.
ASKER CERTIFIED SOLUTION
Avatar of ALaRiva
ALaRiva
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Camillia

ASKER

And that's fine but my coworker says "

This variable declaration hides Property, consider changing the name or removing the declaration.

I dont know what that means. I don't have a private declaration like "private int? _Ethnic"...
is that what he means??
yes, and I would declare it outside of the property.
private int? Ethnic = null;

private int? Ethnic
    {
        get
        {
            if (!string.IsNullOrEmpty(ddlEthnic.SelectedValue)) //not a required field
                Ethnic = int.Parse(ddlEthnic.SelectedValue);

            return Ethnic;
        }

           }
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i think that private int? Ethnic = null;

should be private int? _Ethnic = null;

Correct?
yes, my bad.
i wanted to accept yours, edemcs, but it came out as "assisted" solution. Not sure how.