Click here to Skip to main content
15,881,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a project that does not seem to behave properly when using auto implemented properties.

When using get/set, it returns a null. But when explicitly telling it what to return it works great. For example, I only need to change this:
public string Symbol { get; set; }


to this:
C#
public string Symbol
{
    get { return _symbol; }
    set { _symbol = value; }
}



And it works great.

Does anyone have an explanation?
Posted
Updated 3-Dec-10 4:06am
v3
Comments
TheyCallMeMrJames 15-Nov-10 11:42am    
make sure you comment on people's answers (don't add an answer yourself) or they won't get an email with your notes. cheers.
wizardzz 15-Nov-10 12:05pm    
Thank you very much, Mr. James, I just noticed that you corrected this for me.
TheyCallMeMrJames 15-Nov-10 15:29pm    
no worries. first one's free but after that it's a million bucks a pop. ;o)

Do you have string _symbol defined? If you do, remove it and see if it works. You might have a collision there (though, that doesn't explain if you're actually getting null).

Basically, just make sure if you're using the shortcut that you're not also declaring the private member.

Cheers.
 
Share this answer
 
v2
Comments
wizardzz 15-Nov-10 11:51am    
You got it, I did not think that this would make a difference to the compiler. I had to change the constructor of this class to use Symbol instead of _symbol now.
No explanation but I just tried it and I had no problem with the auto implemented version.
 
Share this answer
 
Comments
TheyCallMeMrJames 15-Nov-10 11:41am    
from OP: Thank you for trying it out Henry.
Maybe it helps to use Reflector[^] and take a look at the generated code when being compiled as an automatic property?

Cheers
Uwe
 
Share this answer
 
v2
Comments
TheyCallMeMrJames 15-Nov-10 11:42am    
from OP: Uwe, I used reflector and the automatic property code looks identical to the code generated by the more explicit method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900