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

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 1346
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*******************@newsreader.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.Name1 = "Some name";
}
}
class B
{
B()
{
MyConstants.Name2 = "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*******************@newsreader.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.MaxInt32 and
Constants.MaxDouble etc.

--
Jon Skeet - <sk***@pobox.com>
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.comwrote in message
news:MP************************@msnews.microsoft.c om...
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.MaxInt32 and
Constants.MaxDouble etc.

--
Jon Skeet - <sk***@pobox.com>
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*******************@newsreader.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.com>
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.comwrote:
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.com>
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.comwrote:
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
John Dalberg <no****@nospam.ssswrote:
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.
Agreed. I still don't think you should be doing what you want to do,
but...

public class Constants // Bad name if they change during runtime
{
public static readonly Name1 = A.ComputeName1();
public static readonly Name2 = B.ComputeName2();
// etc
}

(You could also make them properties rather than public fields.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 16 '07 #11
Ken
I used a module to do this, I know someone declared it seemed to be the
"Old" way - Although I did not wish to make them read only and only I wanted
to persist my variables.
I think that this is one way you could do it..

I hope this helps.
Apr 26 '07 #12

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

Similar topics

4
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...
134
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...
6
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...
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...
2
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,...
7
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...
18
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
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....
4
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;...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.