
Equivalent of "VB Modules" in C#
Jeff,
Quote:
> I have an application in which I would like to construct a
> file which contains a number of constants that can be used
> across several classes/projects.
So, in .Net lingo, you want to create a "Shared Assembly" and install that
assembly in the "Global Assembly Cache." The assembly will contain the
stuff that you want to share across several classes/projects.
Quote:
> I would like to be able to make use of these constants
> without declaring a new instance of the class which
> contains them, as it appears everything must be in a class
> wrapper in C#.
Hum, <scratching head>, hum, <scratching head more>, I am not sure I
understand the significance of this requirement? What is the difference
between declaring them within the context of a class:
public class GlobalConstantsSupplier
{
public const int MyConstant1 = 1;
public const int MyConstant2 = 2;
Quote:
}
and declaring them so that they are not within the context of a class:
const int MyConstant1 = 1;
const int MyConstant2 = 2;
What am I missing? Except that one approach involves object-oriented
programming ( and requires a little more typing) and the other involves
conventional programming (and less typing)?
Quote:
> Is there any way I can do this?
There are many ways to do this, but it takes a paridgm-shift.
Quote:
> I'm guessing there is
> something related to the use of the 'static' keyword which
> can be applied here.
Note that constants are always static (although you don't use the static
keyword) and can not be changed. Statics are static, but can be changed
when they are declared public.
Quote:
> I can get static METHODS to work,
If you want to provide "constants" who's values change depending on some
criteria, the best way to accomplish the task is to declare the variables as
private or protected statics, then provide public static property getter
methods. In those methods you would perform the calculations and return the
constant value. If the value never changes, such as PI, then a public
constant is probably what you are looking for.
Quote:
> but
> I don't know how to declare the CONSTANTS so that they are
> externally visible without constructing an instance of the
> class first.
The example above provide this. Also, consider the "Math" class of the .Net
Framework. It provides PI, E, Abs(), etc., that are examples of what you
might be wanting.
Quote:
> VB.NET has to be doing this somehow in its modules files.
I would recommend that you look to examples found in the classes of the .Net
Framework which provide common functionality for VB.Net, C#, C++, and
JScript. This will reinforce the notion that the CTS and CLR were created
to support a language-agnostic programming environment.