global variables?

D

Daniel Bass

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.
 
D

Daniel Bass

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?
 
D

Dmitriy Lapshin [C# / .NET MVP]

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.
 
P

Peter Vidler

how ridiculous...

Not really.. global variables are really bad practise.
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.

You can have an enumeration outside a class (they are like structs
themselves).. it must still be in a namespace, but that's no real drawback.
who makes up this crap?

People smarter than either of us.

Pete
 
D

Daniel Bass

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_DefaultLoggingLevel = LOG_BASIC;

CLog()
{
// contruction code here
}

CLog( int nLoggingLevel )
{
m_DefaultLoggingLevel = nLoggingLevel;
}

// rest of the class where some method may use m_DefaultLoggingLevel

}


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

void CreateNewLog()
{
CLog myLog(LOG_NORMAL);
}


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 said:
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 said:
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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
R

Rob Tillie

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 said:
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_DefaultLoggingLevel = LOG_BASIC;

CLog()
{
// contruction code here
}

CLog( int nLoggingLevel )
{
m_DefaultLoggingLevel = nLoggingLevel;
}

// rest of the class where some method may use
m_DefaultLoggingLevel

}


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

void CreateNewLog()
{
CLog myLog(LOG_NORMAL);
}


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 said:
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 said:
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.
 
D

Daniel Bass

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)
 
J

Jon Skeet

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.Normal rather than a global name
of LOG_NORMAL) and we have more readable names.

What exactly was the problem again?
 
D

Daniel Pratt

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_DefaultLoggingLevel =
eLOGGING_LEVEL.BASIC;

CLog()
{
// contruction code here
}

CLog(eLOGGING_LEVEL nLoggingLevel )
{
m_DefaultLoggingLevel = nLoggingLevel;
}

// rest of the class where some method may use
m_DefaultLoggingLevel
}

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
 
S

Syanide

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...
 
D

Daniel Bass

Still disagree.

I understand OO, to the put of having a degree in it, as well as other
external courses, sorry I don't add the little letters to my name, and I
know those who do, do it so that when they help out, people know they're
getting professional help, and that's a good thing. I am new to C# and what
it has to offer, and feel that being raw in your problems and why you think
it doesn't work is the best way to learn. I still don't like it, but please
don't make that a personal statement people, I don't like it, it's like you
saying that you don't like englishmen, and even though I am one, I wouldn't
get my back up about it.

It's not a perfect principle, and needs refinement, because it's not a
perfect world... in the real world, when developing large scale products,
you find that they are designed to a release point, then evolve as different
criteria arise. It's horrible to have to work on an project, then get told
from above that the application has to meet additional criteria. We should
then go back to the drawing board, and redesign it, then recode it. But if
you're limited to a serious time restraint and have deadlines to meet, you
need to work with what you have. Hey it's not ideal at all, actually it's
pretty aweful.

The point I'm making is that to restrict the developer to something that
they should not do because it's not proper, good, ethical, morally right, is
no feasible, for two main reasons that I see:
1. there are situations that arise where these ideals will need to be
broken...
2. so when these situations do arise, which you all must face, we have to
developer "neat" ways (!!!???) with working around these rules to produce,
in the end, the same result.

I'm all for good coding standards, and training people to work from good
design, and to develop great design principles, but it's not going to make
everyone who uses it write good neat code... Restricting someone who needs a
back door quick hack may suddenly think something like the following:
I need class G to access some value in class A, now they're not really
related at all, but for a quick hack I end up inheritting G from A, then
making the members protected and static. This obviously SUCKS for more
reasons than there are letters in this post, but it's valid syntax. So does
that mean the next OO is going to stop the user from doing this, because it
doesn't apply to the G IsA A principle... of course not.

Maybe one day I'll change my mind, but I'll stick with C++ for now thanks.
;o)

Are these the same clever people that said we'd only ever need a couple of
bytes for the year of a date? Sure it was essential stuff then, but over
time became rediculous as a concept.

So please, don't get offended that we don't live in the real world, and I
don't particularly like the pure OO programming lanuguage approach, even
though I like OO.

Anyway, thanks for the input, I've learnt a lot. Like the Logging example
was a bad one. ;o) Oh and I'm new to VB too...
 
D

Daniel Bass

Sorry Jon, bad example... the point of what you just put done is that the
value of the member, LogLevel level, can be changed by any method within
that object.
So you get another developer come along, and it's not immediately clear this
is a constant by the structure (because i know if you commented well 100% of
the time it should always be clear).

is there a way to do something like this?

namespace Constants
{

#MAX_BUFFER_SIZE 215

}


where i can just use the "using" operator to include my namespace and then
access hash defined values, or create constants in a similar fashion?

Thanks again! =o)

Daniel.
(Prv 27:17)
 
J

Jon Skeet

Daniel Bass said:
Sorry Jon, bad example... the point of what you just put done is that the
value of the member, LogLevel level, can be changed by any method within
that object.

Yes - although you could make it readonly instead.
So you get another developer come along, and it's not immediately clear this
is a constant by the structure (because i know if you commented well 100% of
the time it should always be clear).

There's a difference between a constant and a readonly variable - in
this case, it sounds like you want it to be readonly, not constant.
is there a way to do something like this?

namespace Constants
{

#MAX_BUFFER_SIZE 215

}

where i can just use the "using" operator to include my namespace and then
access hash defined values, or create constants in a similar fashion?

No - but you could certainly have a class called Constants, which
contained a member called MaxBufferSize. That way you could *also* have
a member called MaxBufferSize in other classes, and they wouldn't
clash.
 
C

Chad Myers

Daniel,

We appreciate your honest. I understand your
point about having this feature because it's
cool, or having that feature, etc.

There are lots of things you could add to
C# for this case or that case, but it just
pollutes the language.

Having a consistent, thorough and respectible
OO implementation is .NET's focus. Implementing
globals would distract from that and lead to
confusion and disproportionate abuse. Anything
you can do with globals you can do without,
so why add inconsistency?

Note that for VB.NET, modules/globals are
a compiler trick. Modules and globals are
turned into static-only classes which get
called in place of the "modules".

-c
 
D

Daniel Goldman

I've considered the same issue. I think there is usually no
way to use global variables in Java or C#. The only way is
to ignore the object oriented features of the languages, and
put a lot of functions into one class as static methods, along
with a bunch of static variables accessed by the static methods.
Perhaps this might be useful in special circumstances.

I agree with Steve McConnell in Code Complete (which ignores OO
programming): Most experienced programmers have concluded that
using global data is riskier than using local data. Most
experienced programmers have also concluded that access to
data from several routines is pretty doggone useful. In spite
of all the hoopla about the dangers of global variables, research
into the hazards of using them has not been conclusive. Even
if global variables don't always produce errors, they're hardly
ever the best way to program.

Daniel Goldman
 
F

Fergus Cooney

Hi Daniel, Evenin' All

This globals debate is about the ownership of names..

Global means truly global - anywhere within my program I can use the
name of a global variable and get the same value. That's great. No
ambiguity. Where's the problem??

The problem is that my program uses a component that someone else, maybe
you Daniel, wrote. The component manages a list of items and the capacity of
the list is given by a global variable which you named, say, MAX_NUM_ITEMS.

Now I've got a list of items and I'd like to have a global called
MAX_NUM_ITEMS. Ah but I can't, because your component owns that name. Still,
that's ok I'll just compromise a bit and call mine _MAX_NUM_ITEMS. A bit
confusing maybe, but your component's already written and I know what I'm
talking about so no problem. [Six months down the line John Hacker, Junior
inherits my project. - He hasn't got as chance. But that's another issue.]

Now my program's getting good. I want to add another component, written
by Jon Skeet ('cos he knows his stuff). His component also manages a list of
items and - hey, guess what - he's got a global called, yep, MAX_NUM_ITEMS.
So I call Daniel and ask him to change his global's name. He refuses because
he's got 100,000 users, all quite happy. So I call Jon and ask him to change
his. He also refuses because he's got 100,000 users, all quite happy.

So now what? Do I junk Jon's component, or dump Daniels's. Do I shoot
myself?

No, no and no again. I use an OO language where it's IMPOSSIBLE for this
kind of crap to occur. [And for other kinds of crap but those are another
issues, too.]

I'm just expanding on a point made by Jon a few posts back. If you're
doing a diddy little program for yourself then globals are easy, cool,
excellent, even efficient. But if you want to integrate the work of discrete
individuals and teams - who aren't even in communication with each other
(and don't need to be) - you need a language which is designed for the job.
It's not about writing utilities anymore - we're building systems.

Just a few thoughts.
Best wishes to all,
Fergus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top