473,734 Members | 2,567 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 66081
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
4368
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
17875
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
5324
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
9168
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
5627
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
3143
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
2657
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
8654
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
11824
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
29371
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
9449
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...
0
9310
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9236
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
8186
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...
1
6735
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 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...
0
4550
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...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
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
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.