Code reivew : Variable hides property?

Camillia
Camillia used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
You declared it as Private and therefore nothing can access it except for the class itself.

Commented:
Oh, and you're redundantly creating a local variable with the same name as the property.  Give your local variable a different name.
Commented:
This line:
int? Ethnic = null;

Because you are declaring type again (int?), you are essentially re-declaring the variable, which will "HIDE" the previous declared version.

What you wanted to do is set the value of the existing declared value, so change the line to this...
Ethnic = null;

hth

- Anthony
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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??

Commented:
yes, and I would declare it outside of the property.

Commented:
private int? Ethnic = null;

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

            return Ethnic;
        }

           }
Commented:
Sorry....


private int? Ethnic = null;

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

            return _Ethnic;
        }

           }

i think that private int? Ethnic = null;

should be private int? _Ethnic = null;

Correct?

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

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