tags:

views:

97

answers:

2

Hey everyone,

I'm converting a VB.net library to C# and having trouble finding the C# equivalent to a VB.Net Public Module.

In the same VB class is a With block. Any idea what the equivalent might be for that as well.

TIA Steve Campos

+2  A: 

There is no With equivalent.

Depending on version, you can do:

var myCommand = new SqlCommand(SpMainInsert, myConnection) {
    CommandType = System.Data.CommandType.StoredProcedure
};

Module Definition

This is the correct definition based on the default declaration when creating a Module (Module template item) in a VB.NET Project. The private constructor is part of the declaration, regardless if it is required or not. This C# "Module Definition" is taken from a Microsoft resource many years back. I do not have reference to the source right now, but will try to post later.

internal { sealed | static } class Module1 {
    #if sealed
        private Module1() { }
    #endif
    public static void Method1() { }
    public static string Method2() { return "here"; }
} 

Calling Module Methods

    Module1.Method1();
    string foo = Module1.Method2();
AMissico
no need for private constructor with static keyword
dkackman
no need for "sealed" with static either, and whether or not "internal" is correct depends on how the original Module is declared.
Joel Coehoorn
This is the correct definition based on the default declaration when creating a `Module` in a VB.NET Project. The private constructor is part of the declaration. This C# "Module Definition" is taken from Microsoft souces many years back. I do not have reference right now, but will try to post later.
AMissico
@AMissico, static classes are implicitly sealed. Adding the sealed keyword is a compile time error. As is adding a private constructor.
JaredPar
@JaredPar: It is just syntax to show the user what a Module looks like in C# and how to make the function calls.
AMissico
@JaredPar: Give it a break, I did not provide sample code.
AMissico
@JaredPar: You happy with pseudo-syntax now?
AMissico
+2  A: 

Unfortunately there is no equivalent of a VB.Net module in C#. The closest thing is a static class. Both define a object which cannot have instance members but they have different semantics. The largest being that for VB.Net if a Module is in scope it's members can be accessed without qualification

Module Example 
  Public Function Sum(x as Integer, y As Integer) As Integer
    return x + y
  End Function
End Module

Class C1
  Sub Method1() 
    Dim x = Sum(13,42)
  End Sub 
Class

C# does not have this feature and requires qualified access for static class members.

There are a couple of other smaller differences

  • Module members are implicitly shared while static class members require explicit static qualifiers
  • Some minor trivia in the structure of the generated types
JaredPar
"The largest being that for VB.Net if a Module is in scope it's members can be accessed without qualification"- This is due to how VB.NET handles namespaces and has nothing to do with "Modules" which are just a class in the "global" namespace. (This is not technically accurate, but not much room in a comment. Check out MSDN for more information.)
AMissico
@AMissico, it has everything to do with Modules. They are the only construct which introduces members into their containing namespace without qualification.
JaredPar
@JaredPar: VB.NET does this introduction, not the module. The module is just ".class public auto ansi sealed <namespace>.<module>". Check the generated IL. The VB.NET Compiler converts the module into a class. There is nothing special about a module. It is the VB.NET Compiler that makes it special.
AMissico
@AMissico, I'm not sure what you're arguing at this poitn. My statement is that VB.Net Modules have semantic differences within VB.Net than C# static classes do within C#. The do compile down to similar structures (not identical, check the attributes) but that does not change the correctness of my statement.
JaredPar
@JaredPar: Then your point was not clear.
AMissico