Camillia
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;
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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??
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(ddl Ethnic.Sel ectedValue )) //not a required field
Ethnic = int.Parse(ddlEthnic.Select edValue);
return Ethnic;
}
}
private int? Ethnic
{
get
{
if (!string.IsNullOrEmpty(ddl
Ethnic = int.Parse(ddlEthnic.Select
return Ethnic;
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
i think that private int? Ethnic = null;
should be private int? _Ethnic = null;
Correct?
should be private int? _Ethnic = null;
Correct?
yes, my bad.
ASKER
i wanted to accept yours, edemcs, but it came out as "assisted" solution. Not sure how.