473,503 Members | 2,152 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Readonly Global Variables?

I know, I know, global variables are bad bad bad.

I have some readonly variables that I assign right away and they will never
change (hence the "readonly" part). I need to access these variables in
multiple classes and I don't want to have multiple declaractions all over
the place and have to remember where to update everything when I decide to
change a value in my code.

Where/how do you do this? Since I'm using ASP.NET, maybe I could put them
in the global.asax? That sounds like a really bad idea though.
Nov 15 '05 #1
5 2352
static?

"blue" <bl**@arizona.edu> wrote in message
news:eO**************@TK2MSFTNGP12.phx.gbl...
I know, I know, global variables are bad bad bad.

I have some readonly variables that I assign right away and they will never change (hence the "readonly" part). I need to access these variables in
multiple classes and I don't want to have multiple declaractions all over
the place and have to remember where to update everything when I decide to
change a value in my code.

Where/how do you do this? Since I'm using ASP.NET, maybe I could put them
in the global.asax? That sounds like a really bad idea though.

Nov 15 '05 #2
mmm... you could use a static class for that... and a static constructor.

--
Telmo Sampaio
MCSE (4 and 2k), MCSA, MCSD (6 and .NET), MCDBA, MCT, SPS, STA, SCSE, SAT,
MSF Practitioner, ITIL Certified
te***********@hotmail.com
"blue" <bl**@arizona.edu> wrote in message
news:eO**************@TK2MSFTNGP12.phx.gbl...
I know, I know, global variables are bad bad bad.

I have some readonly variables that I assign right away and they will never change (hence the "readonly" part). I need to access these variables in
multiple classes and I don't want to have multiple declaractions all over
the place and have to remember where to update everything when I decide to
change a value in my code.

Where/how do you do this? Since I'm using ASP.NET, maybe I could put them
in the global.asax? That sounds like a really bad idea though.

Nov 15 '05 #3
Blue,

If the values are constants, then you can declare public constants on a
class and then they can be accessed anywhere that the class is available.
If you need to do some computation before setting the read-only variable
(for example, say you want to store the machine name, it can't be a
constant, but it is constant throughout the lifetime of the app). You can
set these variables in a static constructor for your type.

Which class you do it in is up to you. Global is a choice. As long as
it is visible to the other types that want to access it.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"blue" <bl**@arizona.edu> wrote in message
news:eO**************@TK2MSFTNGP12.phx.gbl...
I know, I know, global variables are bad bad bad.

I have some readonly variables that I assign right away and they will never change (hence the "readonly" part). I need to access these variables in
multiple classes and I don't want to have multiple declaractions all over
the place and have to remember where to update everything when I decide to
change a value in my code.

Where/how do you do this? Since I'm using ASP.NET, maybe I could put them
in the global.asax? That sounds like a really bad idea though.

Nov 15 '05 #4
Thanks for the ideas. I decided to make an abstract Globals class and I
made the variables public, readonly and static. I made them readonly rather
than constant because our coding standards say that we should only use
constant for well known constant types (like the days of the week) and
everything that is specific to our project should be readonly.

Now I know the difference between readonly and constant.
"blue" <bl**@arizona.edu> wrote in message
news:eO**************@TK2MSFTNGP12.phx.gbl...
I know, I know, global variables are bad bad bad.

I have some readonly variables that I assign right away and they will never change (hence the "readonly" part). I need to access these variables in
multiple classes and I don't want to have multiple declaractions all over
the place and have to remember where to update everything when I decide to
change a value in my code.

Where/how do you do this? Since I'm using ASP.NET, maybe I could put them
in the global.asax? That sounds like a really bad idea though.

Nov 15 '05 #5
Telmo Sampaio <te***********@hotmail.com> wrote:
mmm... you could use a static class for that... and a static constructor.


What exactly do you mean by "a static class"? There's no such thing in
C#, as far as I know.

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

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

Similar topics

4
24159
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
17
5591
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
33
2989
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
19
7697
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...
0
5545
by: Brian Young | last post by:
Hi all. I'm using the Property Grid control in a control to manage a windows service we have developed here. The windows service runs a set of other jobs that need to be managed. The control...
9
8613
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
5
11799
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
11
1353
by: John Dalberg | last post by:
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...
1
29307
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
0
7287
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,...
0
7349
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...
1
7008
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...
0
5594
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,...
1
5022
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...
0
4688
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...
0
3177
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...
1
746
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
399
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...

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.