473,883 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

global variables?

how do i declare a global variable in c#.net? it's like it want's everything
in classes... there are times when globals are good, like having constants
in a program which apply to several layers/objects/etc...

or does it expect me to create a singleton global class structure?

surely it's not that terrible.
Nov 15 '05 #1
25 66088
how ridiculous...

they purposefully instantiate a language so that anything global is
restricted. So if you want something like a global enumeration, that's a
constant through out the application, you have to "work around" their
restriction using statics/singletons or whatever else you can, to achieve
the result they're saying you're not supposed to have.

who makes up this crap?

"Rob Tillie" <Ro********@stu dent.tul.edu> wrote in message
news:Oe******** ******@TK2MSFTN GP09.phx.gbl...
Global variables aren't OO and thus not allowed.
You should declare your constants in a class where it belongs.
You can declare a field static, then it is accessable without instantiating the class:

public static readonly string APP_DIR = "Myconstant ";

Greetz,
-- Rob.

Daniel Bass wrote:
how do i declare a global variable in c#.net? it's like it want's
everything in classes... there are times when globals are good, like
having constants in a program which apply to several
layers/objects/etc...

or does it expect me to create a singleton global class structure?

surely it's not that terrible.


Nov 15 '05 #2
Hello Daniel,

Speaking of globally accessible constants, a common practice is to group
them into sealed classes having private constructors:

public sealed class FieldLimit
{
private FieldLimit()
{
}

public const int FirstName = 90;
public const int Email = 64;
}

Hope this is not terrible and it, in my opinion, makes your code look more
readable.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Unit Testing and Integration Environment
http://x-unity.miik.com.ua
Deliver reliable .NET software

"Daniel Bass" <da********@NOS PAMpostmaster.c o.uk> wrote in message
news:uP******** ******@TK2MSFTN GP12.phx.gbl...
how do i declare a global variable in c#.net? it's like it want's everything in classes... there are times when globals are good, like having constants
in a program which apply to several layers/objects/etc...

or does it expect me to create a singleton global class structure?

surely it's not that terrible.


Nov 15 '05 #3
But then you can't do something like...

enum eLOGGING_LEVEL
{
LOG_BASIC = 0,
LOG_NORMAL = LOG_BASIC + 1,
LOG_EXHAUSTIVE = LOG_NORMAL + 1,
} eLOGGING_LEVEL;

class CLog
{
int m_DefaultLoggin gLevel = LOG_BASIC;

CLog()
{
// contruction code here
}

CLog( int nLoggingLevel )
{
m_DefaultLoggin gLevel = nLoggingLevel;
}

// rest of the class where some method may use m_DefaultLoggin gLevel

}
Now some other class which contains a CLog object wants to do this...

void CreateNewLog()
{
CLog myLog(LOG_NORMA L);
}
The problem being if you have to wrap up all yours apps constants into a
class, you can't use them when initialising variables in those classes.

At least in VB you have an option to put it in a Module... C++ just lets you
do whatever, and perhaps is a bit loose, but people should be taught the
proper way of doing things, rather than stopping everyone from doing
something they think is bad style.

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.c om> wrote
in message news:O7******** ******@TK2MSFTN GP12.phx.gbl...
Hello Daniel,

Speaking of globally accessible constants, a common practice is to group
them into sealed classes having private constructors:

public sealed class FieldLimit
{
private FieldLimit()
{
}

public const int FirstName = 90;
public const int Email = 64;
}

Hope this is not terrible and it, in my opinion, makes your code look more
readable.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Unit Testing and Integration Environment
http://x-unity.miik.com.ua
Deliver reliable .NET software

"Daniel Bass" <da********@NOS PAMpostmaster.c o.uk> wrote in message
news:uP******** ******@TK2MSFTN GP12.phx.gbl...
how do i declare a global variable in c#.net? it's like it want's

everything
in classes... there are times when globals are good, like having constants in a program which apply to several layers/objects/etc...

or does it expect me to create a singleton global class structure?

surely it's not that terrible.

Nov 15 '05 #4
Daniel,

What do you mean by a global enumeration? An enumeration is a type just
like anything else and does not have to be defined in a class (because it IS
a class), and you can then use it anywhere the assembly exporting it is
referenced. That sounds pretty global to me.

Also, as for your constants, you just make them static on a class, and
then you can access those constants anywhere that the class is accessible.

--
- Nicholas Paldino [.NET/C# MVP]
- ni************* *@exisconsultin g.com

"Daniel Bass" <da********@NOS PAMpostmaster.c o.uk> wrote in message
news:Oe******** ******@TK2MSFTN GP09.phx.gbl...
how ridiculous...

they purposefully instantiate a language so that anything global is
restricted. So if you want something like a global enumeration, that's a
constant through out the application, you have to "work around" their
restriction using statics/singletons or whatever else you can, to achieve
the result they're saying you're not supposed to have.

who makes up this crap?

"Rob Tillie" <Ro********@stu dent.tul.edu> wrote in message
news:Oe******** ******@TK2MSFTN GP09.phx.gbl...
Global variables aren't OO and thus not allowed.
You should declare your constants in a class where it belongs.
You can declare a field static, then it is accessable without

instantiating
the class:

public static readonly string APP_DIR = "Myconstant ";

Greetz,
-- Rob.

Daniel Bass wrote:
how do i declare a global variable in c#.net? it's like it want's
everything in classes... there are times when globals are good, like
having constants in a program which apply to several
layers/objects/etc...

or does it expect me to create a singleton global class structure?

surely it's not that terrible.



Nov 15 '05 #5
It's just the same as in every OO language.
Java has the same thing.
In C++, you could do everything, because you didn't have to program the OO
way.

Greetz,
-- Rob.

Daniel Bass wrote:
But then you can't do something like...

enum eLOGGING_LEVEL
{
LOG_BASIC = 0,
LOG_NORMAL = LOG_BASIC + 1,
LOG_EXHAUSTIVE = LOG_NORMAL + 1,
} eLOGGING_LEVEL;

class CLog
{
int m_DefaultLoggin gLevel = LOG_BASIC;

CLog()
{
// contruction code here
}

CLog( int nLoggingLevel )
{
m_DefaultLoggin gLevel = nLoggingLevel;
}

// rest of the class where some method may use
m_DefaultLoggin gLevel

}
Now some other class which contains a CLog object wants to do this...

void CreateNewLog()
{
CLog myLog(LOG_NORMA L);
}
The problem being if you have to wrap up all yours apps constants
into a class, you can't use them when initialising variables in those
classes.

At least in VB you have an option to put it in a Module... C++ just
lets you do whatever, and perhaps is a bit loose, but people should
be taught the proper way of doing things, rather than stopping
everyone from doing something they think is bad style.

"Dmitriy Lapshin [C# / .NET MVP]" <x-****@no-spam-please.hotpop.c om>
wrote in message news:O7******** ******@TK2MSFTN GP12.phx.gbl...
Hello Daniel,

Speaking of globally accessible constants, a common practice is to
group them into sealed classes having private constructors:

public sealed class FieldLimit
{
private FieldLimit()
{
}

public const int FirstName = 90;
public const int Email = 64;
}

Hope this is not terrible and it, in my opinion, makes your code
look more readable.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Unit Testing and Integration Environment
http://x-unity.miik.com.ua
Deliver reliable .NET software

"Daniel Bass" <da********@NOS PAMpostmaster.c o.uk> wrote in message
news:uP******** ******@TK2MSFTN GP12.phx.gbl...
how do i declare a global variable in c#.net? it's like it want's
everything in classes... there are times when globals are good,
like having constants in a program which apply to several
layers/objects/etc...

or does it expect me to create a singleton global class structure?

surely it's not that terrible.

Nov 15 '05 #6
Oh well, the battle goes on, let's all follow java cus sun must be right...

pure OO? like saying there's a perfect way of building applications. sounds
great, but just isn't practical, and can make everything look more bent out
of shape.

thanks for your comments! appreciated. =o)
Nov 15 '05 #7
Daniel Bass <da********@NOS PAMpostmaster.c o.uk> wrote:

<snip>
The problem being if you have to wrap up all yours apps constants into a
class, you can't use them when initialising variables in those classes.


Why not?

For the record, the clean C# way of doing what you were talking about
was:

public enum LogLevel
{
// No need to prepend LOG_ to each name, as the name is already
// qualified by LogLevel - see why it's nice not to have globals?
Basic,
Normal,
Exhaustive
}

public class Logger
{
// Note using the enum type instead of just "int"
LogLevel level;

public Logger()
{
level = LogLevel.Normal ;
}

public Logger (LogLevel level)
{
this.level = level;
}
}

In the other class:

void CreateNewLog()
{
Logger myLog = new Logger(LogLevel .Normal);
}

So here, we have more type safety (using LogLevel instead of int), we
have qualification of names (LogLevel.Norma l rather than a global name
of LOG_NORMAL) and we have more readable names.

What exactly was the problem again?

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

Your code, translated to C# (I'll resist the temptation to ".NET" your
naming conventions):

enum eLOGGING_LEVEL
{
BASIC = 0,
NORMAL = BASIC + 1,
EXHAUSTIVE = NORMAL + 1,
}

class CLog
{
eLOGGING_LEVEL m_DefaultLoggin gLevel =
eLOGGING_LEVEL. BASIC;

CLog()
{
// contruction code here
}

CLog(eLOGGING_L EVEL nLoggingLevel )
{
m_DefaultLoggin gLevel = nLoggingLevel;
}

// rest of the class where some method may use
m_DefaultLoggin gLevel
}

Now some other class which contains a CLog object wants to do this...

void CreateNewLog()
{
CLog myLog(eLOGGING_ LEVEL.NORMAL);
}

To me, this does not seem so *horrible*.

Now, the other thing you should know is that a VB.NET module is
implemented as a sealed class where all the members are "static". The only
other difference is that VB promotes the module members into the global
address space, so you are not required to prefix the members with the class
name, as you would in C#.

Regards,
Dan

"Daniel Bass" <da********@NOS PAMpostmaster.c o.uk> wrote in message
news:eR******** ******@TK2MSFTN GP10.phx.gbl...
But then you can't do something like...

enum eLOGGING_LEVEL
{
LOG_BASIC = 0,
LOG_NORMAL = LOG_BASIC + 1,
LOG_EXHAUSTIVE = LOG_NORMAL + 1,
} eLOGGING_LEVEL;

class CLog
{
int m_DefaultLoggin gLevel = LOG_BASIC;

CLog()
{
// contruction code here
}

CLog( int nLoggingLevel )
{
m_DefaultLoggin gLevel = nLoggingLevel;
}

// rest of the class where some method may use m_DefaultLoggin gLevel

}
Now some other class which contains a CLog object wants to do this...

void CreateNewLog()
{
CLog myLog(LOG_NORMA L);
}
The problem being if you have to wrap up all yours apps constants into a
class, you can't use them when initialising variables in those classes.

At least in VB you have an option to put it in a Module... C++ just lets you do whatever, and perhaps is a bit loose, but people should be taught the
proper way of doing things, rather than stopping everyone from doing
something they think is bad style.

Nov 15 '05 #9
LOL.. Looks like we have a VB programmer here... If you need globals m8..
you also need a proper programming course as well.. There is no need for
globals in any app you right...
if you want a class to be used that contains constant values create a class
with static methods...
And get your static values from that...
"Daniel Bass" <da********@NOS PAMpostmaster.c o.uk> wrote in message
news:Oe******** ******@TK2MSFTN GP09.phx.gbl...
how ridiculous...

they purposefully instantiate a language so that anything global is
restricted. So if you want something like a global enumeration, that's a
constant through out the application, you have to "work around" their
restriction using statics/singletons or whatever else you can, to achieve
the result they're saying you're not supposed to have.

who makes up this crap?

"Rob Tillie" <Ro********@stu dent.tul.edu> wrote in message
news:Oe******** ******@TK2MSFTN GP09.phx.gbl...
Global variables aren't OO and thus not allowed.
You should declare your constants in a class where it belongs.
You can declare a field static, then it is accessable without

instantiating
the class:

public static readonly string APP_DIR = "Myconstant ";

Greetz,
-- Rob.

Daniel Bass wrote:
how do i declare a global variable in c#.net? it's like it want's
everything in classes... there are times when globals are good, like
having constants in a program which apply to several
layers/objects/etc...

or does it expect me to create a singleton global class structure?

surely it's not that terrible.



Nov 15 '05 #10

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

Similar topics

1
4375
by: mark4asp | last post by:
What are the best methods for using global constants and variables? I've noticed that many people put all global constants in a file and include that file on every page. This is the best way of doing it - is it not? Once the application has loaded the page it is cached and is immediately available for other pages. With global variables - the best thing to do would be to use application variables - so long as there weren't too many...
10
17900
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can overwrite another copies global variable? I know that you could overwrite a value in a global variable from a function, but you could also do that if you pass the variable in and then out again... so how is that any different?
5
5331
by: Richard A. DeVenezia | last post by:
Dear Experts: Suppose I have global variables: x1, x2, x3 and I have a function that needs to assign a value to a new global variable x4 something like function foo () { count = 0;
2
9176
by: Patient Guy | last post by:
I have a library of functions representing a filesystem interface (essentially a file selection interface, to be used in opening/reading/writing/closing files). Heavily scripted HTML document #1, very application-like, must include the JS file for this library of functions. One reason is that it must call a function to name a "callback" function, and within its own script block, must define the callback function. The callback handles...
17
5648
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 constantly running tests on imperfect code. On of the cumbersome jobs encountered is reassigning global vars their values after a close encounter with an untrapped runtime error. Rather than writing a procedure to simply reassign them all with a...
7
3151
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a couple of function that all need the same information (all located in the same file). By now it looks like /* file beginns */
10
2672
by: Charles O'Flynn | last post by:
As a complete newcomer (2-3 days) to PHP, although not to programming in general, I have 'dived in' to start a small project to read and parse an XML data stream. I have already worked out most of the more specialist aspects of the job but am now completely stuck on something I would have thought were simplicity itself... I need to have a large number of global variables visible inside functions - it's not possible to pass them into the...
9
8678
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 Su Gatlin, casual mention was made about using static variables as an alternative to using global variables. This caused me to think of the following: '-----Begin module code
5
11839
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 $MYVAR, $a; ?>
1
29413
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 called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
0
9792
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,...
0
11142
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10847
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,...
0
10416
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9574
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7129
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4612
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
2
4220
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3233
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.