Equivalent of "VB Modules" in C# 
Author Message
 Equivalent of "VB Modules" in C#

Jeff,

    It's simple.  All you have to do is declare your properties/fields as
either static (which allows them to be changed) or const (which they can not
be changed) or as static readonly (which can only be changed when the static
constructor is called, or they use the value that they are defined with).
You can do it like this:

// Const, static, and static readonly values.
public const int MyConst = 1;
public static readonly int MyStaticReadOnly = 2;
public static int MyStatic = 3;

    Then compile this into an assembly and add a reference to this assembly
wherever you need it in your projects.

    Hope this helps.

--
               - Nicholas Paldino [.NET/C# MVP]


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.

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

> Is there any way I can do this?  I'm guessing there is
> something related to the use of the 'static' keyword which
> can be applied here. I can get static METHODS to work, but
> I don't know how to declare the CONSTANTS so that they are
> externally visible without constructing an instance of the
> class first.

> VB.NET has to be doing this somehow in its modules files.
> What is it doing behind the scenes in order to make this
> happen, such that I can emulate it in C#.

> Thanks for any replies.

> Jeff Ulrich



Sat, 14 May 2005 00:02:54 GMT  
 Equivalent of "VB Modules" in C#


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.

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

> Is there any way I can do this?  I'm guessing there is
> something related to the use of the 'static' keyword which
> can be applied here. I can get static METHODS to work, but
> I don't know how to declare the CONSTANTS so that they are
> externally visible without constructing an instance of the
> class first.

> VB.NET has to be doing this somehow in its modules files.
> What is it doing behind the scenes in order to make this
> happen, such that I can emulate it in C#.

> Thanks for any replies.

> Jeff Ulrich

Constants are static by their very nature.  All a VB Module is, is a class
with all static methods, and a private constructor.  You can do the same in
C#....

public class Globals
{

    // here are some constants
    public const int Red = 0;
    public const int Blue = 1;
    public const int Green = 2;

    // here are some readonly stuff
    public static readonly Guid IID = new
Guid("148BD524-A2AB-11CE-B11F-00AA00530503");

    // here is a method
    public static bool DoCoolStuff()
    {
        return true;
    }

    private Globals() {} // make sure no one can create the class

Quote:
}

HTH,
Tom Shelton


Sat, 14 May 2005 00:21:55 GMT  
 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.


Sat, 14 May 2005 00:54:25 GMT  
 Equivalent of "VB Modules" in C#
Once again, thank you, this is exactly what I wanted to do.

Jeff

Quote:
>-----Original Message-----
>Jeff,

>    It's simple.  All you have to do is declare your

properties/fields as
Quote:
>either static (which allows them to be changed) or const
(which they can not
>be changed) or as static readonly (which can only be

changed when the static
Quote:
>constructor is called, or they use the value that they
are defined with).
>You can do it like this:

>// Const, static, and static readonly values.
>public const int MyConst = 1;
>public static readonly int MyStaticReadOnly = 2;
>public static int MyStatic = 3;

>    Then compile this into an assembly and add a

reference to this assembly
Quote:
>wherever you need it in your projects.

>    Hope this helps.

>--
>               - Nicholas Paldino [.NET/C# MVP]



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

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

>> Is there any way I can do this?  I'm guessing there is
>> something related to the use of the 'static' keyword
which
>> can be applied here. I can get static METHODS to work,
but
>> I don't know how to declare the CONSTANTS so that they
are
>> externally visible without constructing an instance of
the
>> class first.

>> VB.NET has to be doing this somehow in its modules
files.
>> What is it doing behind the scenes in order to make this
>> happen, such that I can emulate it in C#.

>> Thanks for any replies.

>> Jeff Ulrich

>.



Sat, 14 May 2005 02:49:48 GMT  
 Equivalent of "VB Modules" in C#
Tom,

Quote:
>All a VB Module is, is a class
>with all static methods, and a private constructor.  You can do the same in
>C#....

Actually, Modules don't have constructors at all, not even a private
one. You can't do that in C#.

Mattias

===

http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.



Sat, 14 May 2005 05:42:51 GMT  
 Equivalent of "VB Modules" in C#


Quote:
> Tom,

> >All a VB Module is, is a class
> >with all static methods, and a private constructor.  You can do the same
in
> >C#....

> Actually, Modules don't have constructors at all, not even a private
> one. You can't do that in C#.

Looks like your right.... I just looked at the IL and it appears that it is
a sealed class with a
Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute applied.  I
knew it was class, I just assumed that it had a private constructor.  Oh,
well you get the same affect by just adding a private constructor to a C#
class with all static methods.

Tom Shelton



Sat, 14 May 2005 06:24:25 GMT  
 Equivalent of "VB Modules" in C#


Quote:



> > Tom,

> > >All a VB Module is, is a class
> > >with all static methods, and a private constructor.  You can do the
same
> in
> > >C#....

> > Actually, Modules don't have constructors at all, not even a private
> > one. You can't do that in C#.

> Looks like your right.... I just looked at the IL and it appears that
it is
> a sealed class with a
> Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute
applied.  I
> knew it was class, I just assumed that it had a private constructor.
Oh,
> well you get the same affect by just adding a private constructor to a
C#
> class with all static methods.

More VB trickery to obscure true object oriented programming :(

-c



Sat, 14 May 2005 08:35:17 GMT  
 Equivalent of "VB Modules" in C#


Quote:





> > > Tom,

> > > >All a VB Module is, is a class
> > > >with all static methods, and a private constructor.  You can do the
> same
> > in
> > > >C#....

> > > Actually, Modules don't have constructors at all, not even a private
> > > one. You can't do that in C#.

> > Looks like your right.... I just looked at the IL and it appears that
> it is
> > a sealed class with a
> > Microsoft.VisualBasic.CompilerServices.StandardModuleAttribute
> applied.  I
> > knew it was class, I just assumed that it had a private constructor.
> Oh,
> > well you get the same affect by just adding a private constructor to a
> C#
> > class with all static methods.

> More VB trickery to obscure true object oriented programming :(

> -c

It does seem odd to me... Why go through all the trouble to add an
attribute, when all they had to do was add a private constructor?

Tom Shelton



Sat, 14 May 2005 06:58:39 GMT  
 Equivalent of "VB Modules" in C#
Tom,

Quote:
>Why go through all the trouble to add an
>attribute, when all they had to do was add a private constructor?

Huh? Those are entirely different things.

Modules don't have constructors because there's no need for them, all
members are shared.

Having a private constructor is *not* the same thing as not having a
constructor at all. If you have a private constructor, the class can
can create instances of itself. You can't to that with a Module.

The StandardModuleAttribute is there so the VB.NET compiler can
differentiate between a regular class and a module. That's needed to
auto-Import module members.

Mattias

===

http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.



Sat, 14 May 2005 07:13:12 GMT  
 Equivalent of "VB Modules" in C#


Quote:
> Tom,

> >Why go through all the trouble to add an
> >attribute, when all they had to do was add a private constructor?

> Huh? Those are entirely different things.

> Modules don't have constructors because there's no need for them, all
> members are shared.

> Having a private constructor is *not* the same thing as not having a
> constructor at all. If you have a private constructor, the class can
> can create instances of itself. You can't to that with a Module.

> The StandardModuleAttribute is there so the VB.NET compiler can
> differentiate between a regular class and a module. That's needed to
> auto-Import module members.

To me the idea was to disallow a client to instantiate an instance of the
Module...  I wasn't thinking along the lines of a class not being able to
instantiate it self.  I suppose it makes a lot more sense when you look at
it that way.  Thanks for clearing that up.

Tom Shelton



Sat, 14 May 2005 07:26:35 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. The equivalent of VB "with"

2. Equivalent class of "Properties" in C#

3. C# equivalent to "nothing"

4. VB "now()" function in C#

5. help for problem of module with "%"

6. C equivalent of "ls *.c"

7. Function ptr equivalent of "NULL"?

8. "Timer"Function in MC or equivalent!!

9. Equivalent of "Inside ATL"

10. "DoEvents" equivalent

11. C++ equivalent of "DoEvents"

12. C++ equivalent of "DoEvents"

 

 
Powered by phpBB® Forum Software