473,569 Members | 2,562 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Readonly kind of variable declared somewhere by defined somewhere else?

I would like to declare variables that can't be modified (like readonly)
however I want to declare them in a single class so I can always refer to
the same class whenever code uses them. However I want to give them values
in constructors in different classes in different files.

What's the best way to accomplish this functionality?

John Dalberg
Mar 12 '07 #1
11 1367
VJ
I still don't fully understand what you are trying accomplish.. its tough to
give efficient OO ideas without a requirement picture. But from what you
say..using static variables in public class is a option. I generally don't
like to use static variables a lot, so if you are thinking of a lot of
variables, then a sketch of requirement picture might help people here to
give better idea.

VJ

"John Dalberg" <no****@nospam. ssswrote in message
news:20******** ***********@new sreader.com...
>I would like to declare variables that can't be modified (like readonly)
however I want to declare them in a single class so I can always refer to
the same class whenever code uses them. However I want to give them values
in constructors in different classes in different files.

What's the best way to accomplish this functionality?

John Dalberg

Mar 12 '07 #2
"VJ" <no***********@ yahoo.comwrote:
I still don't fully understand what you are trying accomplish.. its tough
to give efficient OO ideas without a requirement picture. But from what
you say..using static variables in public class is a option. I generally
don't like to use static variables a lot, so if you are thinking of a lot
of variables, then a sketch of requirement picture might help people here
to give better idea.

I want to do something like this:

class static MyConstants
{
public readonly Name1;
public readonly Name2;
}
class A
{
A()
{
MyConstants.Nam e1 = "Some name";
}
}
class B
{
B()
{
MyConstants.Nam e2 = "Some name";
}
}

The idea is to consolidate all non modifiable variable in one class but
give them an initial values in other classes.

John Dalberg
---------------------------------------------------------


>
VJ

"John Dalberg" <no****@nospam. ssswrote in message
news:20******** ***********@new sreader.com...
I would like to declare variables that can't be modified (like readonly)
however I want to declare them in a single class so I can always refer
to the same class whenever code uses them. However I want to give them
values in constructors in different classes in different files.

What's the best way to accomplish this functionality?

John Dalberg
Mar 14 '07 #3
John Dalberg <no****@nospam. ssswrote:

<snip>
The idea is to consolidate all non modifiable variable in one class but
give them an initial values in other classes.
That sounds like a bad idea to me. Do the variables have anything in
common other than being read-only? It doesn't sound like it.

Constants (and the like) are best expressed in their related classes -
so we have int.MaxValue and double.MaxValue , not Constants.MaxIn t32 and
Constants.MaxDo uble etc.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 14 '07 #4
VJ
Singleton Pattern will help here?

VJ

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
John Dalberg <no****@nospam. ssswrote:

<snip>
>The idea is to consolidate all non modifiable variable in one class but
give them an initial values in other classes.

That sounds like a bad idea to me. Do the variables have anything in
common other than being read-only? It doesn't sound like it.

Constants (and the like) are best expressed in their related classes -
so we have int.MaxValue and double.MaxValue , not Constants.MaxIn t32 and
Constants.MaxDo uble etc.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mar 14 '07 #5
VJ
Ok my bad.. no singleton.. coffee time of the day...Sorry

Anyways what John is doing seems to be ok.. why is it bad to keep
application wide constants in a common class?

"John Dalberg" <no****@nospam. ssswrote in message
news:20******** ***********@new sreader.com...
>I would like to declare variables that can't be modified (like readonly)
however I want to declare them in a single class so I can always refer to
the same class whenever code uses them. However I want to give them values
in constructors in different classes in different files.

What's the best way to accomplish this functionality?

John Dalberg

Mar 14 '07 #6
VJ <no***********@ yahoo.comwrote:
Ok my bad.. no singleton.. coffee time of the day...Sorry

Anyways what John is doing seems to be ok.. why is it bad to keep
application wide constants in a common class?
Because all they have in common is that they're constants. If they were
all configuration parameters, then it would make sense to keep them in
a Configuration class, because that describes their purpose. However,
"constant" describes one particular attribute of the value, rather than
what the purpose of the value is.

I mean, it'll *work*, but it doesn't scale well as the project scales
up - you end up having to use very long names because part of the name
describes the general area of the constant, which would be a good
candidate for a class in which to put the constant.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 15 '07 #7
Jon Skeet [C# MVP] <sk***@pobox.co mwrote:
VJ <no***********@ yahoo.comwrote:
Ok my bad.. no singleton.. coffee time of the day...Sorry

Anyways what John is doing seems to be ok.. why is it bad to keep
application wide constants in a common class?

Because all they have in common is that they're constants. If they were
all configuration parameters, then it would make sense to keep them in
a Configuration class, because that describes their purpose. However,
"constant" describes one particular attribute of the value, rather than
what the purpose of the value is.
They are like configuration variables but their values are determined
during runtime.
>
I mean, it'll *work*, but it doesn't scale well as the project scales
up - you end up having to use very long names because part of the name
describes the general area of the constant, which would be a good
candidate for a class in which to put the constant.
Scaling is not an issue because the app is not going to get big. Why is
long names an issue? The number of variables will not exceed 50. Don't
names convert to some random short names in IL regardless of how long the
name was in the code?

You said they are a good candidate to be in a class. Isn't that the same
what I was proposing? I want all these va riables in be in a custom class
so that I can refer to them using a single class name with the help of
intellisense instead of remembering which class contains them if they are
going to be scattered in different classes.

John Dalberg
Mar 15 '07 #8
John Dalberg <no****@nospam. ssswrote:
Because all they have in common is that they're constants. If they were
all configuration parameters, then it would make sense to keep them in
a Configuration class, because that describes their purpose. However,
"constant" describes one particular attribute of the value, rather than
what the purpose of the value is.

They are like configuration variables but their values are determined
during runtime.
If they're not configurable, I wouldn't keep them together then.
I mean, it'll *work*, but it doesn't scale well as the project scales
up - you end up having to use very long names because part of the name
describes the general area of the constant, which would be a good
candidate for a class in which to put the constant.

Scaling is not an issue because the app is not going to get big. Why is
long names an issue? The number of variables will not exceed 50. Don't
names convert to some random short names in IL regardless of how long the
name was in the code?
No, it's a readability issue rather than anything else.
You said they are a good candidate to be in a class. Isn't that the same
what I was proposing? I want all these va riables in be in a custom class
so that I can refer to them using a single class name with the help of
intellisense instead of remembering which class contains them if they are
going to be scattered in different classes.
*If* they're logically linked as configuration parameters, that makes
sense. It doesn't if they're logically a constant which more reasonably
belongs to another class - which is suggested by the fact that you want
to initialise them from other classes.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 15 '07 #9
Jon Skeet [C# MVP] <sk***@pobox.co mwrote:
John Dalberg <no****@nospam. ssswrote:
Because all they have in common is that they're constants. If they
were all configuration parameters, then it would make sense to keep
them in a Configuration class, because that describes their purpose.
However, "constant" describes one particular attribute of the value,
rather than what the purpose of the value is.
They are like configuration variables but their values are determined
during runtime.

If they're not configurable, I wouldn't keep them together then.
They are changed during runtime which means they are configurable.
>
I mean, it'll *work*, but it doesn't scale well as the project scales
up - you end up having to use very long names because part of the
name describes the general area of the constant, which would be a
good candidate for a class in which to put the constant.
Scaling is not an issue because the app is not going to get big. Why is
long names an issue? The number of variables will not exceed 50. Don't
names convert to some random short names in IL regardless of how long
the name was in the code?

No, it's a readability issue rather than anything else.
Readability... long names has nothing to do with my issue. I don't
understand why you even bring this up. Anyways, it's all subjective. A 20
character name can be readible for one, too long for someone else.

>
You said they are a good candidate to be in a class. Isn't that the
same what I was proposing? I want all these va riables in be in a
custom class so that I can refer to them using a single class name
with the help of intellisense instead of remembering which class
contains them if they are going to be scattered in different classes.

*If* they're logically linked as configuration parameters, that makes
sense. It doesn't if they're logically a constant which more reasonably
belongs to another class - which is suggested by the fact that you want
to initialise them from other classes.

I don't see this discussion going to my desirable outcome. It's becoming
more like an argument than "this is how you do it" solution. Thanks for
your input.

John Dalberg
Mar 16 '07 #10

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

Similar topics

4
4147
by: Chris Beall | last post by:
If you want your code to be bulletproof, do you have to explicitly check for the existence of any possibly-undefined variable? Example: window.outerHeight is defined by some browsers, but not others. It would therefore seem prudent, before using this variable, to do something like: if (typeof (window.outerHeight) != "undefined") { do stuff...
134
7796
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that means that if I misspell a variable name, my program will mysteriously fail to work with no error message. If you don't declare variables, you...
6
10115
by: adamrfrench | last post by:
Let it be mentioned that Javascript is not my forte, so the solution to this could very well be a simple one. I am working on an AJAX function where I can pass a URL and the target ID in, and have the function update the target ID with the URL. There is a bit more to it then that, but that is the basics. my difficulty comes when I try to...
19
7700
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...
2
1341
by: Nick Stansbury | last post by:
Hi, I'm not sure where to post this - but I'll try here. I'm encountering some really weird behaviour in visual studio and I can't work out where it is coming from. In various files, seemingly at random, the compiler seems to "forget" about references declared in the code behind file. So when I build I get a whole string of errors like...
7
2469
by: DareDevil | last post by:
I have written a method that should modify the folder path passed to it into one that exists and is selected by the user. It then returns a boolean depending on whether a folder path was selected by the user It then dawned on me that I was passing in a readonly property into the method yet neither at compile time or runtime was I getting any kind...
18
4029
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
0
2030
by: zman77 | last post by:
EDIT: -- forgot to mention... I am using Visual Studio 2005, on Win XP, on an intel machine Hi. This is my first post, though I've "lurked" for a while because I find these forums very helpful. Ok my problem is the following. I have a class that contains a "MakeByteArray" function. I have many objects of that class. Inside that...
4
2144
by: cody | last post by:
It is possible to declare and use/instantiate a class with a uninitialized readonly field without even a compiler warning. Why don't I get warnings? public class Stuff { public readonly int a; } By definition, readonly fields can only be initialized inside a
0
7701
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, 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...
0
7615
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...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8130
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7979
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 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...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2115
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
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.