C# equivalent of VB Public Module, With too please
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 开发者_StackOverflowis a With block. Any idea what the equivalent might be for that as well.
TIA Steve Campos
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
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();
精彩评论