VB“模块"的C#等效项 [英] C# Equivalent for VB 'module'
问题描述
在Visual Basic中,您可以将模块用作存储松散"代码的位置,这些代码可以是可以从应用程序中的其他位置访问的方法和变量,而无需首先初始化某些东西,并且可以设置或更改变量状态.进行了更改,并将在整个过程中继续保持该价值.
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.
我发现最接近的是C#中作为公共类的一部分的静态方法,但是这样做的缺点是无法全局访问变量,或者如果将变量设置为静态,则在内部可设置/获取表.
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.
以VB中存储在空白模块中的以下简单代码为例.
Take for example the following simple code in VB stored in a blank module.
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
现在,您有了一个课程,然后可以从该课程中这样调用CheckModulus()
Now, you have a class, and from that class, you can then call CheckModulus()
as such
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
现在,我意识到,可以将iCount移至设置",并在应用程序启动时将其重置,等等,但是请记住,这是一个非常简单的示例,它说明了能够有一套全局代码.我发现过去最有用的地方是创建UserControls或自定义类时.此外,其目的不是使所有内容都可以全局访问,而是使某些方法和变量可以全局访问,而其他方法和变量只能从模块内部访问.例如,虽然CheckModulus()
和Increment()
(全局方法)都可以修改并获取iCount
值,但iCount
不能全局访问,就像模块中私有定义的方法一样.
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 iCount
value, iCount
is not accessible globally, as would the way be with private defined methods in the module.
所以,最让人吃惊的是:
So the big pickle is this :
C#中与VB&中的功能等效的代码类型是什么? VB.NET的
module
吗?
What is the functionally equivalent code type in C# to VB & VB.NET's
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.
如果没有功能上等效的东西,那么什么样的机灵 hack或解决方法(除了使用设置或外部存储设备外) 例如注册表,数据库,文件等)来实现此目的,或者 东西很近吗?
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 ?
推荐答案
您可以使用静态构造函数进行初始化.
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
}
}
并像这样访问它:
MyStuff.SomeVariable = "hello";
MyStuff.SomeListOfStuff.Add("another item for the list");
这篇关于VB“模块"的C#等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!