Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageI 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?
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.