protected variables or protected accessors?

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

I've got a base class with some protected variables in it,
for use by any sub-classes. This all works fine, but I
have just run the assembly through FxCop and it suggests
making the variables private and adding protected property
accessors to it. I can't see the argument for this. Can
anyone clarify this?

Thanks

John
 
John said:
I've got a base class with some protected variables in it,
for use by any sub-classes. This all works fine, but I
have just run the assembly through FxCop and it suggests
making the variables private and adding protected property
accessors to it. I can't see the argument for this. Can
anyone clarify this?

It's the same as for not making variables public - you get more
flexibility with properties:

o You can change the type of the underlying variable later on
without affecting compatibility (i.e. expose the old property as
well as probably a new one)
o You can have a setter but no getter or vice versa
o You can provide error checking
o You may end up doing something entirely different (e.g. get a
value from the registry, or use a cached version most of the time,
or something like that)

Other than the second one, all of these can be done later on, when
you've got code which depends on the interface you're providing. If
you've just got a variable, you don't get any of that flexibility.
 
Back
Top