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

ASP.NET

Avatar of undefined
Last Comment
Camillia
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.
Avatar of edemcs
edemcs
Flag of United States of America image

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Camillia
Camillia
Flag of United States of America image

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??
Avatar of edemcs
edemcs
Flag of United States of America image

yes, and I would declare it outside of the property.
Avatar of edemcs
edemcs
Flag of United States of America image

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
Avatar of edemcs
edemcs
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Camillia
Camillia
Flag of United States of America image

ASKER

i think that private int? Ethnic = null;

should be private int? _Ethnic = null;

Correct?
Avatar of edemcs
edemcs
Flag of United States of America image

yes, my bad.
Avatar of Camillia
Camillia
Flag of United States of America image

ASKER

i wanted to accept yours, edemcs, but it came out as "assisted" solution. Not sure how.
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo