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.
}
} 2 2164
Ranier Dunno <an*******@disc ussions.microso ft.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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too -----Original Message----- Ranier Dunno <an*******@disc ussions.microso ft.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 constructor?
Example:
public class MyClass
{
|
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 from binary incompatibility
since it is hardbaked into the binary.
I do not believe there is a performance advantage with using const over
static readonly since the JIT will take account of that or am I wrong here?
|
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, whereas with
fieldInfos = typeof(DaClass).GetFields(BindingFlags.Static);
|
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 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.
|
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 wanna use global.asax) ---
Would you declare your static var as ---
public static int x ;
| |
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 not valid for this
item.
Why? Are static classes not allowed??
|
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 occurences.
i have been looking at both static and const variables as the options,
but couldn't decide on one.
|
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 method. How ever, when I am debugging, that method is not
being called. So I am not able to figure out, whether the field is
getting initialized properly or not. Please explain this behavior
Thanks in advance
|
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 modify come of
the classes so they match our purpose better - mostly add a few
methods etc.
Example: Open source lib has classes Map and Layer defined. The
relationship between them is one to many.
We created our own versions (inherited) of Map...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |