vb.net C# 等效于 VB“模块”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30870487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Equivalent for VB 'module'
提问by Kraang Prime
In Visual Basic, you can use a module as a place to store 'loose' code which can be methods and variables that are accessible from elsewhere in the application without having to first initialize something, and the variable states can be set or changed and will continue to keep that value throughout.
在 Visual Basic 中,您可以使用模块作为存储“松散”代码的地方,这些代码可以是可以从应用程序中的其他地方访问的方法和变量,而无需先初始化某些东西,并且可以设置或更改变量状态,并将在整个过程中继续保持这个价值。
The closest I have found, is static methods in C# as part of a public class, however this has the drawback of variables which are not globally accessible, or internally settable/gettable if the variables are made static.
我发现的最接近的是 C# 中的静态方法作为公共类的一部分,但是这具有变量不可全局访问的缺点,或者如果变量是静态的,则可以在内部设置/获取。
Take for example the following simple code in VB stored in a blank module.
以存储在空白模块中的 VB 中的以下简单代码为例。
Private iCount as Integer = 0
Public Sub Increment()
iCount = iCount + 1
End Sub
Public CheckModulus() As Boolean
If iCount % 6 == 0 Then
Return True
Else
Return False
End If
End Sub
Now, you have a class, and from that class, you can then call CheckModulus()as such
现在,你有一个类,并从类,你可以调用CheckModulus()这样
Public Class Fruits
Public Static Function ExactBunches() As String
If CheckModulus() Then
Return "You have an exact amount of bunches"
Else
Return "You need more fruits to make a bunch"
End If
End Function
End Class
Now I realize with some hack and slash, that you could move iCount to 'settings', and reset it on application launch, etc, but please bear in mind this is a very simple example to illustrate the convenience of being able to have a set of global code. Where I have found this most useful in the past is when creating UserControls or custom classes. In addition, the intent is not to make everything globally accessable, but to have certain methods and variables globally accessable while others remain ONLY accessible from within the module. For example, while CheckModulus()and Increment()(global methods) both have access to modify and obtain the iCountvalue, iCountis not accessible globally, as would the way be with private defined methods in the module.
现在我通过一些技巧和斜线意识到,您可以将 iCount 移至“设置”,并在应用程序启动时重置它,等等,但请记住,这是一个非常简单的示例,用于说明能够拥有设置的便利性全局代码。我发现这在过去最有用的地方是在创建用户控件或自定义类时。此外,目的不是让所有东西都可以全局访问,而是让某些方法和变量可以全局访问,而其他方法和变量只能从模块内部访问。例如,whileCheckModulus()和Increment()(全局方法)都可以访问修改和获取iCount值,但iCount不能全局访问,就像模块中的私有定义方法一样。
So the big pickle is this :
所以大泡菜是这样的:
What is the functionally equivalent code type in C# to VB & VB.NET's
module?
什么是 C# 中与 VB 和 VB.NET 功能等效的代码类型
module?
Due to the complex nature of this simple question, I feel I should impose a boolean for a 'just in case there is no answer' answer as follows.
由于这个简单问题的复杂性,我觉得我应该为“以防万一没有答案”的答案强加一个布尔值,如下所示。
If, there is nothing functionally equivalent, then what sort of clever hack or workaround (aside from using settings, or external storage like the registry, database, files, etc), to make this happen or something VERY very close ?
如果没有任何功能等效的东西,那么有什么巧妙的技巧或解决方法(除了使用设置或外部存储,如注册表、数据库、文件等)来实现这一点或非常接近?
回答by DavidG
You can use a static class. You can also initialise these using a static constructor.
public static class MyStuff
{
//A property
public static string SomeVariable { get; set; }
public static List<string> SomeListOfStuff { get; set; }
//Init your variables in here:
static MyStuff()
{
SomeVariable = "blah";
SomeListOfStuff = new List<string>();
}
public static async Task<string> DoAThing()
{
//Do your async stuff in here
}
}
And access it like this:
并像这样访问它:
MyStuff.SomeVariable = "hello";
MyStuff.SomeListOfStuff.Add("another item for the list");
回答by Matt Burland
A static class like this would be equivalent to your VB code:
像这样的静态类相当于您的 VB 代码:
public static class MyModule
{
private static int iCount = 0; // this is private, so not accessible outside this class
public static void Increment()
{
iCount++;
}
public static bool CheckModulus()
{
return iCount % 6 == 0;
}
// this part in response to the question about async methods
// not part of the original module
public async static Task<int> GetIntAsync()
{
using (var ms = new MemoryStream(Encoding.ASCII.GetBytes("foo")))
{
var buffer = new byte[10];
var count = await ms.ReadAsync(buffer, 0, 3);
return count;
}
}
}
You would then call it like this (and the value of iCountdoes persist because it's static):
然后您可以这样调用它(并且 的值iCount确实存在,因为它是静态的):
// iCount starts at 0
Console.WriteLine(MyModule.CheckModulus()); // true because 0 % 6 == 0
MyModule.Increment(); // iCount == 1
Console.WriteLine(MyModule.CheckModulus()); // false
MyModule.Increment(); // 2
MyModule.Increment(); // 3
MyModule.Increment(); // 4
MyModule.Increment(); // 5
MyModule.Increment(); // 6
Console.WriteLine(MyModule.CheckModulus()); // true because 6 % 6 == 0
Console.WriteLine(MyModule.GetIntAsync().Result); // 3
A fiddle- updated with an async static method
一个小提琴-与异步静态方法更新

