473,382 Members | 1,745 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,382 software developers and data experts.

inherited static readonly fields

Hi,

I have a class hierarchy, and would like all classes in
the tree to have a common, static readonly variable -
with a class-specific value. I'd like it to be readonly
(it should never change), but this prevents me from
setting its value in the static constructor of the
subclasses - I'm allowed to set its value in the
superclass constructor only.

Any suggestions as to what would be a good solution?

An example is provided below:

public class A
{
public static readonly string MyString;

static A
{
MyString = "Hello, World!"; // This is OK.
}
}

public class B : A
{
static B
{
MyString = "Bonjour, Le Monde!"; // This is NOT.
}
}
Nov 15 '05 #1
2 2156
Ranier Dunno <an*******@discussions.microsoft.com> wrote:
I have a class hierarchy, and would like all classes in
the tree to have a common, static readonly variable -
with a class-specific value.
That's not going to happen, basically.
I'd like it to be readonly
(it should never change), but this prevents me from
setting its value in the static constructor of the
subclasses - I'm allowed to set its value in the
superclass constructor only.
It wouldn't help you even if you could set it in your derived type's
static constructor - if you declare a static variable in the base type,
there's only one variable however many derived types you have.
Any suggestions as to what would be a good solution?


Have a map from type (or typename) to value instead, and make each
class set the value for itself in its static constructor. If you make
the map itself private and give a public readonly property to fetch the
value, and a protected method to write the value, you should be okay.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
-----Original Message-----
Ranier Dunno <an*******@discussions.microsoft.com> wrote:
I have a class hierarchy, and would like all classes in the tree to have a common, static readonly variable -
with a class-specific value.
That's not going to happen, basically.
I'd like it to be readonly
(it should never change), but this prevents me from
setting its value in the static constructor of the
subclasses - I'm allowed to set its value in the
superclass constructor only.


It wouldn't help you even if you could set it in your

derived type'sstatic constructor - if you declare a static variable in the base type,there's only one variable however many derived types you have.

D'oh! Hadn't even thought about that problem :/
Any suggestions as to what would be a good solution?
Have a map from type (or typename) to value instead, and

make eachclass set the value for itself in its static constructor. If you makethe map itself private and give a public readonly property to fetch thevalue, and a protected method to write the value, you

should be okay.
Brilliant workaround! You the man! :-D
Nov 15 '05 #3

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

Similar topics

3
by: Dave | last post by:
Hi everyone, Is it possible, using an Attribute or by some other means, to notify the C# Compiler to serialize all static field's that have initializers before code in an explicit static...
19
by: cody | last post by:
Iam wondering what the benefit of using const over static readonly is. static readonly is a runtime constant and can be set once in the initializer or the static ctor, whereas const is suffering...
3
by: ChristianREMOVE.EitnerTHIS | last post by:
Hi people, I have a class which defines some "public static readonly string" fields. If I say FieldInfo fieldInfos = typeof(DaClass).GetFields(); these fields are reported by reflection,...
9
by: Reuben | last post by:
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...
25
by: Sahil Malik [MVP] | last post by:
So here's a rather simple question. Say in an ASP.NET application, I wish to share common constants as static variables in global.asax (I know there's web.config bla bla .. but lets just say I...
5
by: Tina | last post by:
I'm using C# 1.1.............. I add new Class item to my project. It's created as class Math I change it to public static class Math I get an error saying that "The modifier 'static' is...
5
by: Sek | last post by:
hi folks, i have a bunch of strings used in my code in many places. these strings reside inside a instantiable class. so, i want to replace these with constant/static variable to control the...
10
by: sunil | last post by:
Hello, I am new to c# . I have some basic programming doubts. Please help me in clarifying these doubts. I want to initialize a static and readonly field with a value returned by a static...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.