Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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:
public string Symbol
{
    get { return _symbol; }
    set { _symbol = value; }
}
 

And it works great.
 
Does anyone have an explanation?
Posted 15-Nov-10 5:05am
wizardzz104.9K
Edited 3-Dec-10 4:06am
v3
Comments
TheyCallMeMrJames at 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 at 15-Nov-10 12:05pm
   
Thank you very much, Mr. James, I just noticed that you corrected this for me.
TheyCallMeMrJames at 15-Nov-10 15:29pm
   
no worries. first one's free but after that it's a million bucks a pop. ;o)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
v2
Comments
wizardzz at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

No explanation but I just tried it and I had no problem with the auto implemented version.
  Permalink  
Comments
TheyCallMeMrJames at 15-Nov-10 11:41am
   
from OP: Thank you for trying it out Henry.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Maybe it helps to use Reflector[^] and take a look at the generated code when being compiled as an automatic property?
 
Cheers
Uwe
  Permalink  
v2
Comments
TheyCallMeMrJames at 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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Guruprasad.K.Basavaraju 490
1 Sergey Alexandrovich Kryukov 316
2 Shai Vashdi 264
3 OriginalGriff 250
4 praveen_07 115
0 Sergey Alexandrovich Kryukov 9,185
1 OriginalGriff 5,315
2 Peter Leow 4,040
3 Maciej Los 3,535
4 Abhinav S 3,308


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 3 Dec 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid