473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Inherited Readonly Members

I have a field in an abstract class marked readonly. However, when I attempt
to modify that field in the constructor of a class which inherits it, I get a
compile-time error stating that 'A readonly field cannot be assigned to
(except in a constructor or a variable initializer).' Any ideas would be
apreciated, though I was hoping I wouldn't have to use a property.
Reuben.
Nov 17 '05 #1
9 2334
It can only be changed in the containing classes constructor, or in its
variable initializer. Private (non readonly) field, protected set method
would do the same, yes?

Regards,

Tim Haughton

"Reuben" <Re****@discussions.microsoft.com> wrote in message
news:6B**********************************@microsof t.com...
I have a field in an abstract class marked readonly. However, when I attempt to modify that field in the constructor of a class which inherits it, I get a compile-time error stating that 'A readonly field cannot be assigned to
(except in a constructor or a variable initializer).' Any ideas would be
apreciated, though I was hoping I wouldn't have to use a property.
Reuben.

Nov 17 '05 #2
"Tim Haughton" <ti*********@gmail.com> wrote in news:tmNye.72339$H46.63876
@fe03.news.easynews.com:
It can only be changed in the containing classes constructor, or in its
variable initializer. Private (non readonly) field, protected set method
would do the same, yes?


You dont need a set method, you can just access the field directly if you declare it protected.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Empower ASP.NET with IntraWeb
http://www.atozed.com/IntraWeb/
Nov 17 '05 #3


Reuben wrote:
I have a field in an abstract class marked readonly. However, when I attempt
to modify that field in the constructor of a class which inherits it, I get a


readonly means that the member can only be assigned to in the
constructor (or by an initialization expression).

But, in your case you can pass it's value to the parent constructor:

class Foo {
readonly int x;
protected Foo(..., int x) { ...; }
}
class Bar: Foo {
public static int calculate_x(object args) { ...; return 0; }
public Bar(..., object args): base(..., calculate_x(args)) { ...; }
}

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Nov 17 '05 #4
Chad Z. Hower aka Kudzu <cp**@hower.org> wrote:
"Tim Haughton" <ti*********@gmail.com> wrote in news:tmNye.72339$H46.63876
@fe03.news.easynews.com:
It can only be changed in the containing classes constructor, or in its
variable initializer. Private (non readonly) field, protected set method
would do the same, yes?


You dont need a set method, you can just access the field directly if
you declare it protected.


Yes, if you don't believe (as I do) that fields should pretty much
*always* be private, with protected properties allowing access to
derived classes :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
You can call the base constructor with a value and initialize it in the base
class.

for example:

public class One
{
private readonly int _number;
public One(int number)
{
_number = number;
}
}

public class Two : One
{
public Two(int number) : base(number)
{
}
}

Hope this helps...

Frisky

"Reuben" <Re****@discussions.microsoft.com> wrote in message
news:6B**********************************@microsof t.com...
I have a field in an abstract class marked readonly. However, when I
attempt
to modify that field in the constructor of a class which inherits it, I
get a
compile-time error stating that 'A readonly field cannot be assigned to
(except in a constructor or a variable initializer).' Any ideas would be
apreciated, though I was hoping I wouldn't have to use a property.
Reuben.

Nov 17 '05 #6
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
Yes, if you don't believe (as I do) that fields should pretty much
*always* be private, with protected properties allowing access to
derived classes :)


Thats not always a good method. There is a more common practice of making fields only private or
protected, and all pulbic interfaces as properties. Applying this a level higher though just adds
codes to descendants in many cases.

--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Get your ASP.NET in gear with IntraWeb!
http://www.atozed.com/IntraWeb/
Nov 17 '05 #7
Chad Z. Hower aka Kudzu <cp**@hower.org> wrote:
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in
news:MP************************@msnews.microsoft.c om:
Yes, if you don't believe (as I do) that fields should pretty much
*always* be private, with protected properties allowing access to
derived classes :)


Thats not always a good method. There is a more common practice of
making fields only private or protected, and all pulbic interfaces as
properties. Applying this a level higher though just adds codes to
descendants in many cases.


Microsoft's guidelines disagree with you :)

http://msdn.microsoft.com/library/de...l=/library/en-
us/cpgenref/html/cpconfieldusageguidelines.asp

"Do not use instance fields that are public or protected (Public or
Protected in Visual Basic). If you avoid exposing fields directly to
the developer, classes can be versioned more easily because a field
cannot be changed to a property while maintaining binary
compatibility."

Basically, the exact same reasons for avoiding public fields apply to
protected fields in my view.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in news:MPG.1d3b4e57c10c97ff98c444
@msnews.microsoft.com:
Microsoft's guidelines disagree with you :)
Its not the only one I disagree with. :) Guidelines are just that, guidelines.

In fact I dont use privates at all - I realize this is heresy to many but we do a lot of inheriting
within our own frameworks and too often I've inherited someone elses object and very few
developers understand when to make something private and when to make it protected - and those
that do rarely sit and think about each declaration. Too often things that are private should be
protected (Fields, methods, properties). If its all protected - it can be inherited from. If you dont
intend your class to be inherited from, seal it.
"Do not use instance fields that are public or protected (Public or
Protected in Visual Basic). If you avoid exposing fields directly to
the developer, classes can be versioned more easily because a field
cannot be changed to a property while maintaining binary
compatibility."


The part they dont state is that doing so in many cases adds significant overhead, and if the
developer really doesnt undersand what they are doing and blindly follows this then those
inheriting from their class will have real problems.

Such advice is fine for final classes, but if its intended to be inherited from its bad advice to be
followed as a rule.

If you look at type datasets, or troll around with reflector you will see that in some cases
Microsoft does not follow consistent guildelins themselves, and certainly when they do they
are not using the same ones they have published for others. I suspect that those that published
these guidelines are different and simply provided some basic starts and tips for others since
many users do not know where to start.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Develop ASP.NET applications easier and in less time:
http://www.atozed.com/IntraWeb/
Nov 17 '05 #9
Chad Z. Hower aka Kudzu <cp**@hower.org> wrote:
Its not the only one I disagree with. :) Guidelines are just that, guidelines.

In fact I dont use privates at all - I realize this is heresy to many
but we do a lot of inheriting within our own frameworks and too often
I've inherited someone elses object and very few developers
understand when to make something private and when to make it
protected - and those that do rarely sit and think about each
declaration. Too often things that are private should be protected
(Fields, methods, properties). If its all protected - it can be
inherited from. If you dont intend your class to be inherited from,
seal it.
Indeed, and I'd argue that classes should be sealed by default just
like methods are non-virtual by default. I find that inheritance should
be used far less frequently than it tends to be.

Versioning makes a huge difference here - if you've got nothing
private, then you pretty much can't refactor anything without breaking
an existing version. I'd find that unnecessarily restrictive.
"Do not use instance fields that are public or protected (Public or
Protected in Visual Basic). If you avoid exposing fields directly to
the developer, classes can be versioned more easily because a field
cannot be changed to a property while maintaining binary
compatibility."


The part they dont state is that doing so in many cases adds
significant overhead, and if the developer really doesnt undersand
what they are doing and blindly follows this then those inheriting
from their class will have real problems.


In some cases it adds significant overhead, but I'd say that's pretty
rare.
Such advice is fine for final classes, but if its intended to be
inherited from its bad advice to be followed as a rule.

If you look at type datasets, or troll around with reflector you will
see that in some cases Microsoft does not follow consistent
guildelins themselves, and certainly when they do they are not using
the same ones they have published for others. I suspect that those
that published these guidelines are different and simply provided
some basic starts and tips for others since many users do not know
where to start.


While I'd agree with most of that, this is one of the guidelines I
would really encourage to be followed on almost every occasion. The
exception for me is for occasional nested classes which are purely
there to encapsulate some data, and serve no purpose outside their
enclosing type. In those cases, having made the type itself private, I
don't feel so bad about making the fields internal so I can reference
them directly. Nothing else will ever be able to use the fields outside
the enclosing class, and it's easy enough to refactor that later on -
without the potential for breaking any other classes.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Michi Henning | last post by:
Hi, I can't find a statement about this in the threading sections in the doc... Consider: class Class1 { Class1() { _val = 42;
5
by: cody | last post by:
I know I can use ArrayList.ReadOnly() to create a readonly version of it, but how can I achive a similar thing with a generic collection of .NET 2.0?
1
by: skootr | last post by:
I have a Public Interface defined in a class module. I also have a form that implements that interface After building the solution, I added an Inherited Form (inherited from the above-mentioned...
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
2
by: lovecreatesbeauty | last post by:
I'm disturbed on this question on a long time. I think if I finally get understand it with your kind help, I will get close to a excellent C++ programmer. And I only can rely on your expertise and...
4
by: asad.naeem | last post by:
hi to all this is the problem about inheritence. I have designed a form with some essential controls which are required for every form which will inherited from it. for example i have Button1 on...
14
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming...
19
by: jan.loucka | last post by:
Hi, We're building a mapping application and inside we're using open source dll called MapServer. This dll uses object model that has quite a few classes. In our app we however need to little bit...
2
by: eggie5 | last post by:
Hi, I have a class Dog which derives from Animal. Suppose my instance of Dog is bulldog. bulldog has all the members of Animal plus Dog. How can copy just the members from Dog to a new Dog...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.