364,033 Members | 4774 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

global variables?

Daniel Bass
P: n/a
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.


Nov 15 '05 #1
Share this Question
Share on Google+
25 Replies


Daniel Bass
P: n/a
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?



"Rob Tillie" <Rob.Tillie@student.tul.edu> wrote in message
news:OeBJrY$ZDHA.2344@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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[/color]
instantiating[color=blue]
> the class:
>
> public static readonly string APP_DIR = "Myconstant";
>
> Greetz,
> -- Rob.
>
> Daniel Bass wrote:[color=green]
> > 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.[/color]
>
>[/color]


Nov 15 '05 #2

Dmitriy Lapshin [C# / .NET MVP]
P: n/a
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.

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

"Daniel Bass" <danielbass@NOSPAMpostmaster.co.uk> wrote in message
news:uPUmKS$ZDHA.1744@TK2MSFTNGP12.phx.gbl...[color=blue]
> how do i declare a global variable in c#.net? it's like it want's[/color]
everything[color=blue]
> 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.
>
>[/color]

Nov 15 '05 #3

Daniel Bass
P: n/a
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 [C# / .NET MVP]" <x-code@no-spam-please.hotpop.com> wrote
in message news:O7y5cf$ZDHA.1004@TK2MSFTNGP12.phx.gbl...[color=blue]
> 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" <danielbass@NOSPAMpostmaster.co.uk> wrote in message
> news:uPUmKS$ZDHA.1744@TK2MSFTNGP12.phx.gbl...[color=green]
> > how do i declare a global variable in c#.net? it's like it want's[/color]
> everything[color=green]
> > in classes... there are times when globals are good, like having[/color][/color]
constants[color=blue][color=green]
> > 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.
> >
> >[/color]
>[/color]


Nov 15 '05 #4

Nicholas Paldino [.NET/C# MVP]
P: n/a
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.

--
- Nicholas Paldino [.NET/C# MVP]
- nicholas.paldino@exisconsulting.com

"Daniel Bass" <danielbass@NOSPAMpostmaster.co.uk> wrote in message
news:OeOiUd$ZDHA.2520@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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" <Rob.Tillie@student.tul.edu> wrote in message
> news:OeBJrY$ZDHA.2344@TK2MSFTNGP09.phx.gbl...[color=green]
> > 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[/color]
> instantiating[color=green]
> > the class:
> >
> > public static readonly string APP_DIR = "Myconstant";
> >
> > Greetz,
> > -- Rob.
> >
> > Daniel Bass wrote:[color=darkred]
> > > 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.[/color]
> >
> >[/color]
>
>[/color]


Nov 15 '05 #5

Rob Tillie
P: n/a
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 Bass wrote:[color=blue]
> 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 [C# / .NET MVP]" <x-code@no-spam-please.hotpop.com>
> wrote in message news:O7y5cf$ZDHA.1004@TK2MSFTNGP12.phx.gbl...[color=green]
>> 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" <danielbass@NOSPAMpostmaster.co.uk> wrote in message
>> news:uPUmKS$ZDHA.1744@TK2MSFTNGP12.phx.gbl...[color=darkred]
>>> 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.[/color][/color][/color]


Nov 15 '05 #6

Daniel Bass
P: n/a
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)


Nov 15 '05 #7

Jon Skeet
P: n/a
Jon Skeet
Daniel Bass <danielbass@NOSPAMpostmaster.co.uk> wrote:

<snip>
[color=blue]
> 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.[/color]

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?

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

Daniel Pratt
P: n/a
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

"Daniel Bass" <danielbass@NOSPAMpostmaster.co.uk> wrote in message
news:eRANdn$ZDHA.2136@TK2MSFTNGP10.phx.gbl...[color=blue]
> 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[/color]
you[color=blue]
> 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.[/color]


Nov 15 '05 #9

Syanide
P: n/a
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...


"Daniel Bass" <danielbass@NOSPAMpostmaster.co.uk> wrote in message
news:OeOiUd$ZDHA.2520@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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" <Rob.Tillie@student.tul.edu> wrote in message
> news:OeBJrY$ZDHA.2344@TK2MSFTNGP09.phx.gbl...[color=green]
> > 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[/color]
> instantiating[color=green]
> > the class:
> >
> > public static readonly string APP_DIR = "Myconstant";
> >
> > Greetz,
> > -- Rob.
> >
> > Daniel Bass wrote:[color=darkred]
> > > 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.[/color]
> >
> >[/color]
>
>[/color]


Nov 15 '05 #10

Peter Vidler
P: n/a
Peter Vidler
> how ridiculous...

Not really.. global variables are really bad practise.
[color=blue]
> 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.[/color]

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.
[color=blue]
> who makes up this crap?[/color]

People smarter than either of us.

Pete


Nov 15 '05 #11

Christian
P: n/a
Christian
Hi all[color=blue]
> Global variables aren't OO and thus not allowed.[/color]
Forgive my ignorance, what does OO mean?

Thanks to all, Chris.


Nov 15 '05 #12

Christian
P: n/a
Christian
Hi all[color=blue]
> Global variables aren't OO and thus not allowed.[/color]
Forgive my ignorance, what does OO mean?

Thanks to all, Chris.


Nov 15 '05 #13

Jon Skeet
P: n/a
Jon Skeet
Christian <pleasenononospamcmillotti@novasoftware.it> wrote:[color=blue][color=green]
> > Global variables aren't OO and thus not allowed.[/color][/color]
[color=blue]
> Forgive my ignorance, what does OO mean?[/color]

Object Oriented

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

Jon Skeet
P: n/a
Jon Skeet
Christian <pleasenononospamcmillotti@novasoftware.it> wrote:[color=blue][color=green]
> > Global variables aren't OO and thus not allowed.[/color][/color]
[color=blue]
> Forgive my ignorance, what does OO mean?[/color]

Object Oriented

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

Daniel Bass
P: n/a
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...


Nov 15 '05 #16

Daniel Bass
P: n/a
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...


Nov 15 '05 #17

Daniel Bass
P: n/a
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)

"Jon Skeet" <skeet@pobox.com> wrote in message
news:MPG.19af074ee73f0f7d98a363@news.microsoft.com ...[color=blue]
> Daniel Bass <danielbass@NOSPAMpostmaster.co.uk> wrote:
>
> <snip>
>[color=green]
> > 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.[/color]
>
> 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?
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet/
> If replying to the group, please do not mail me too[/color]


Nov 15 '05 #18

Daniel Bass
P: n/a
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)

"Jon Skeet" <skeet@pobox.com> wrote in message
news:MPG.19af074ee73f0f7d98a363@news.microsoft.com ...[color=blue]
> Daniel Bass <danielbass@NOSPAMpostmaster.co.uk> wrote:
>
> <snip>
>[color=green]
> > 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.[/color]
>
> 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?
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet/
> If replying to the group, please do not mail me too[/color]


Nov 15 '05 #19

Jon Skeet
P: n/a
Jon Skeet
Daniel Bass <danielbass@NOSPAMpostmaster.co.uk> wrote:[color=blue]
> 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.[/color]

Yes - although you could make it readonly instead.
[color=blue]
> 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).[/color]

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.
[color=blue]
> 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?[/color]

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.

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

Jon Skeet
P: n/a
Jon Skeet
Daniel Bass <danielbass@NOSPAMpostmaster.co.uk> wrote:[color=blue]
> 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.[/color]

Yes - although you could make it readonly instead.
[color=blue]
> 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).[/color]

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.
[color=blue]
> 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?[/color]

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.

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

james
P: n/a
james
There are too many C# people anyway, enjoy the '80 's :)

JIM


"Daniel Bass" <danielbass@NOSPAMpostmaster.co.uk> wrote in message
news:OXMhSlIaDHA.1580@tk2msftngp13.phx.gbl...[color=blue]
> 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[/color]
what[color=blue]
> it has to offer, and feel that being raw in your problems and why you[/color]
think[color=blue]
> it doesn't work is the best way to learn. I still don't like it, but[/color]
please[color=blue]
> don't make that a personal statement people, I don't like it, it's like[/color]
you[color=blue]
> saying that you don't like englishmen, and even though I am one, I[/color]
wouldn't[color=blue]
> 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[/color]
different[color=blue]
> 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,[/color]
is[color=blue]
> 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[/color]
a[color=blue]
> 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[/color]
does[color=blue]
> that mean the next OO is going to stop the user from doing this, because[/color]
it[color=blue]
> 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...
>
>[/color]


Nov 15 '05 #22

Chad Myers
P: n/a
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

"Daniel Bass" <danielbass@NOSPAMpostmaster.co.uk> wrote in message
news:OXMhSlIaDHA.1580@tk2msftngp13.phx.gbl...[color=blue]
> 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[/color]
I[color=blue]
> know those who do, do it so that when they help out, people know[/color]
they're[color=blue]
> getting professional help, and that's a good thing. I am new to C# and[/color]
what[color=blue]
> it has to offer, and feel that being raw in your problems and why you[/color]
think[color=blue]
> it doesn't work is the best way to learn. I still don't like it, but[/color]
please[color=blue]
> don't make that a personal statement people, I don't like it, it's[/color]
like you[color=blue]
> saying that you don't like englishmen, and even though I am one, I[/color]
wouldn't[color=blue]
> 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[/color]
products,[color=blue]
> you find that they are designed to a release point, then evolve as[/color]
different[color=blue]
> criteria arise. It's horrible to have to work on an project, then get[/color]
told[color=blue]
> from above that the application has to meet additional criteria. We[/color]
should[color=blue]
> then go back to the drawing board, and redesign it, then recode it.[/color]
But if[color=blue]
> you're limited to a serious time restraint and have deadlines to meet,[/color]
you[color=blue]
> need to work with what you have. Hey it's not ideal at all, actually[/color]
it's[color=blue]
> pretty aweful.
>
> The point I'm making is that to restrict the developer to something[/color]
that[color=blue]
> they should not do because it's not proper, good, ethical, morally[/color]
right, is[color=blue]
> 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[/color]
to[color=blue]
> developer "neat" ways (!!!???) with working around these rules to[/color]
produce,[color=blue]
> in the end, the same result.
>
> I'm all for good coding standards, and training people to work from[/color]
good[color=blue]
> design, and to develop great design principles, but it's not going to[/color]
make[color=blue]
> everyone who uses it write good neat code... Restricting someone who[/color]
needs a[color=blue]
> 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,[/color]
then[color=blue]
> 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[/color]
does[color=blue]
> that mean the next OO is going to stop the user from doing this,[/color]
because it[color=blue]
> 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[/color]
thanks.[color=blue]
> ;o)
>
> Are these the same clever people that said we'd only ever need a[/color]
couple of[color=blue]
> bytes for the year of a date? Sure it was essential stuff then, but[/color]
over[color=blue]
> time became rediculous as a concept.
>
> So please, don't get offended that we don't live in the real world,[/color]
and I[color=blue]
> don't particularly like the pure OO programming lanuguage approach,[/color]
even[color=blue]
> though I like OO.
>
> Anyway, thanks for the input, I've learnt a lot. Like the Logging[/color]
example[color=blue]
> was a bad one. ;o) Oh and I'm new to VB too...
>
>[/color]


Nov 15 '05 #23

Daniel Goldman
P: n/a
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

"Chad Myers" <cmyers@N0.SP.AM.austin.rr.com> wrote in message news:<Ou5l$eNaDHA.2072@TK2MSFTNGP10.phx.gbl>...[color=blue]
> 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
>
> "Daniel Bass" <danielbass@NOSPAMpostmaster.co.uk> wrote in message
> news:OXMhSlIaDHA.1580@tk2msftngp13.phx.gbl...[color=green]
> > 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[/color]
> I[color=green]
> > know those who do, do it so that when they help out, people know[/color]
> they're[color=green]
> > getting professional help, and that's a good thing. I am new to C# and[/color]
> what[color=green]
> > it has to offer, and feel that being raw in your problems and why you[/color]
> think[color=green]
> > it doesn't work is the best way to learn. I still don't like it, but[/color]
> please[color=green]
> > don't make that a personal statement people, I don't like it, it's[/color]
> like you[color=green]
> > saying that you don't like englishmen, and even though I am one, I[/color]
> wouldn't[color=green]
> > 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[/color]
> products,[color=green]
> > you find that they are designed to a release point, then evolve as[/color]
> different[color=green]
> > criteria arise. It's horrible to have to work on an project, then get[/color]
> told[color=green]
> > from above that the application has to meet additional criteria. We[/color]
> should[color=green]
> > then go back to the drawing board, and redesign it, then recode it.[/color]
> But if[color=green]
> > you're limited to a serious time restraint and have deadlines to meet,[/color]
> you[color=green]
> > need to work with what you have. Hey it's not ideal at all, actually[/color]
> it's[color=green]
> > pretty aweful.
> >
> > The point I'm making is that to restrict the developer to something[/color]
> that[color=green]
> > they should not do because it's not proper, good, ethical, morally[/color]
> right, is[color=green]
> > 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[/color]
> to[color=green]
> > developer "neat" ways (!!!???) with working around these rules to[/color]
> produce,[color=green]
> > in the end, the same result.
> >
> > I'm all for good coding standards, and training people to work from[/color]
> good[color=green]
> > design, and to develop great design principles, but it's not going to[/color]
> make[color=green]
> > everyone who uses it write good neat code... Restricting someone who[/color]
> needs a[color=green]
> > 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,[/color]
> then[color=green]
> > 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[/color]
> does[color=green]
> > that mean the next OO is going to stop the user from doing this,[/color]
> because it[color=green]
> > 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[/color]
> thanks.[color=green]
> > ;o)
> >
> > Are these the same clever people that said we'd only ever need a[/color]
> couple of[color=green]
> > bytes for the year of a date? Sure it was essential stuff then, but[/color]
> over[color=green]
> > time became rediculous as a concept.
> >
> > So please, don't get offended that we don't live in the real world,[/color]
> and I[color=green]
> > don't particularly like the pure OO programming lanuguage approach,[/color]
> even[color=green]
> > though I like OO.
> >
> > Anyway, thanks for the input, I've learnt a lot. Like the Logging[/color]
> example[color=green]
> > was a bad one. ;o) Oh and I'm new to VB too...
> >
> >[/color][/color]
Nov 15 '05 #24

Fergus Cooney
P: n/a
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


Nov 15 '05 #25

25 Replies

Post your reply

Help answer this question



Didn't find the answer to your C# / C Sharp question?

You can also browse similar questions: C# / C Sharp c# global c# global:: c# globals global c# globals c#