C# equivalent of VB "Module"

  • Thread starter Thread starter BH
  • Start date Start date
B

BH

Hi

what would be the C# equivalent of a VB.NET file that looks like this:

Module Utilities

Public Sub UtilitySubOne ( ByVal arg As String)
// blah blah
End Sub

Public Sub UtilitySubTwo ( ByVal arg As String)
// blah blah
End Sub

End Module

....VB.NET has the concept of a 'Module' where you can create a pile of
generally available calls, but I'm not sure how this 'Module' file idea
translates to C#.

Any help appreciated,

BH
 
Hi BH,

It would look something like this:

sealed class Utilities
{
private Utilities(){}

public static void UtilitySubOne(string arg)
{
// blah blah
}

...
}

In other words, VB modules are implemented as sealed classes with a
private constructor where all the members are static. I think that VB also
promotes the member names into the global namespace, which means you can
type:

...
UtilitySubOne("foo")
...

instead of:

...
Utilities.UtilitySubOne("foo")
...

The former isn't something you can do in C#.

Regards,
Dan
 
The module concept is just a hack put in to make it easier for VB6 users to
migrate to dot net. It goes against the grain of .NET programming in
general; it's really idiosyncratic to VB. So I wouldn't worry to much about
it.

Chris
 
Cmon Chris...that's not true. All a module is is a specific class...so you
don't have to re-invent it each time you need to use one. One advantage
they have is that they can be referenced without a direct reference which
can make your code a little more concise. Not a huge benefit by any means
but noteworthy nonetheless.

If they really went against everything .NET...why does a single instance
sealed class with all static members have IL that's a dead ringer for a
Module's IL?
 
Daniel's class shows this very clearly, but all a module is is a Sealed
class with all static members
 
One advantage
they have is that they can be referenced without a direct reference which
can make your code a little more concise. Not a huge benefit by any means
but noteworthy nonetheless.

That's the only benefit they offer against the sealed class, static method
C# equivalent, you're right. And as far as VB modules are only the
equivalent of the C# version, they don't go against .NET philosophy. All I
meant was: since you don't have to use the name of the class, it seems
Modules are just a syntactical simulation of global functions. And global
functions are definately soooo three years ago.

:-)

Chris
 
Back
Top