For Programmers: Free Programming Magazines  


Home > Archive > C# > May 2005 > Global consts not allowed?!









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Global consts not allowed?!
Aleko

2005-05-03, 8:57 pm

I just discovered that C# won't let me declare a constant at the
namespace level. My app has a group of classes that need to share
string constants, and it seems like perfectly reasonable thing to
declare these at the namespace level. Apparently, Microsoft thought
otherwise.

This seems like a totally unnecessary, and frustrating constraint. Why
are namespace constants disallowed, but namespace enums are not? An
enum is conceptually the same thing (with the inconvenient restriction
of being only integer based).

This anti-feature adds no value to the language, and just forces
programmers to resort to cumbersome workarounds. If you really want
global constants you can still achieve the effect by declaring them as
static members of a struct. The resulting syntax looks a lot like
enums.

Now that I've vented my frustration I feel better, so I'll go an
structurize all my constants.

Aleko

Bruce Wood

2005-05-03, 8:57 pm

I was surprised by this, too. However, I find a year on that I have
very little use for "free-floating" constants that aren't associated
with any class. Most constants belong as part of a class definition,
just for the sake of clarity.

I say "most". On odd occasion I have a constant or two that really
don't belong with any particular class, but need to be globally
accessible. That's when I make them members of a sealed utility class.

I you have that many free-floating, global constants, I have trouble
believing that none of them logically belongs with any class. If, as
you say, they're specific only to one particular application, then why
not put them in the class that holds your Main method?

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com