Properties vs. Fields
am 24.01.2008 21:47:29 von Mark Henry
I've run into a strange problem that I'm having a difficult time
tracking down. I have no idea what's causing it, so this seemed as good
a place to ask as any...
If anyone is familiar with Neo (entity object framework), it deals with
that. Basically, I have two versions of the code.
version 1:
private readonly EnterpriseCodeValueDefinitionRelation
_enterpriseCodeValueDefinitions;
public EnterpriseCodeValueDefinitionRelation EnterpriseCodeValueDefinitions
{
get
{
return _enterpriseCodeValueDefinitions;
}
}
and version 2:
public readonly EnterpriseCodeValueDefinitionRelation
EnterpriseCodeValueDefinitions;
In each case, the readonly field is intialized in the ctor.
Version 1 works, whereas Version 2 produces really strange behavior
where a later ASP page ends up binding to incorrect data - the object
are of the wrong type (EnterpriseCodeValueDefinition instead of
EnterpriseCodeDefinition).
Basically, can anyone think of a scenario where accessing a field
through a property will cause different behavior then accessing the
field directly? Even if the field is readonly? And if anyone has Neo
experience, is this something specific to Neo (which wouldn't surprise
me in the least - I need to hunt down their mailing lists)?
-Mark-
Re: Properties vs. Fields
am 24.01.2008 22:58:38 von skeet
Mark Henry wrote:
> I've run into a strange problem that I'm having a difficult time
> tracking down. I have no idea what's causing it, so this seemed as good
> a place to ask as any...
> Basically, can anyone think of a scenario where accessing a field
> through a property will cause different behavior then accessing the
> field directly? Even if the field is readonly? And if anyone has Neo
> experience, is this something specific to Neo (which wouldn't surprise
> me in the least - I need to hunt down their mailing lists)?
Well, data binding may well treat them very differently...
--
Jon Skeet -
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Re: Properties vs. Fields
am 25.01.2008 16:38:06 von schneider
One example:
The private field member value is not changed until the property set method
is completed.
I get caught by this once in a while...
Schneider
"Jon Skeet [C# MVP]" wrote in message
news:MPG.22031a6d159ba4db807@msnews.microsoft.com...
> Mark Henry wrote:
>> I've run into a strange problem that I'm having a difficult time
>> tracking down. I have no idea what's causing it, so this seemed as good
>> a place to ask as any...
>
>
>
>> Basically, can anyone think of a scenario where accessing a field
>> through a property will cause different behavior then accessing the
>> field directly? Even if the field is readonly? And if anyone has Neo
>> experience, is this something specific to Neo (which wouldn't surprise
>> me in the least - I need to hunt down their mailing lists)?
>
> Well, data binding may well treat them very differently...
>
> --
> Jon Skeet -
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> World class .NET training in the UK: http://iterativetraining.co.uk
Re: Properties vs. Fields
am 25.01.2008 16:42:59 von schneider
Correction:
One example:
The public property Get method will not report the new private field member
value until the Property set method has completed.
Schneider
"schneider" wrote in message
news:OdBxIh2XIHA.5448@TK2MSFTNGP04.phx.gbl...
> One example:
> The private field member value is not changed until the property set
> method is completed.
>
> I get caught by this once in a while...
>
> Schneider
>
>
>
> "Jon Skeet [C# MVP]" wrote in message
> news:MPG.22031a6d159ba4db807@msnews.microsoft.com...
>> Mark Henry wrote:
>>> I've run into a strange problem that I'm having a difficult time
>>> tracking down. I have no idea what's causing it, so this seemed as good
>>> a place to ask as any...
>>
>>
>>
>>> Basically, can anyone think of a scenario where accessing a field
>>> through a property will cause different behavior then accessing the
>>> field directly? Even if the field is readonly? And if anyone has Neo
>>> experience, is this something specific to Neo (which wouldn't surprise
>>> me in the least - I need to hunt down their mailing lists)?
>>
>> Well, data binding may well treat them very differently...
>>
>> --
>> Jon Skeet -
>> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
>> World class .NET training in the UK: http://iterativetraining.co.uk
>
>