VB.Net模块行为(VB.Net module behavior)
我的VB.Net模块有一个“奇怪”的情况,根据我的理解,VB.Net中的
Module
意味着静态类,所以我实现了几个辅助模块,每个模块都有几个函数,让我们举一些例子来更好地解释(免费手码,可能包含语法问题):Namespace Helpers Module HelperA Public Function FunctionA() As Boolean Return True End Function End Module End Namespace Namespace Helpers Module HelperB Public Function FunctionB() As Integer Return 1 End Function End Module End Namespace
当我在Visual Studio中开始编码并键入
Helpers.
,FunctionA()
和FunctionB()
都显示在推荐的自动完成对话框中,我还没有输入HelperA
或HelperB
,我有一些静态类的C#.Net项目,我发现这种行为不适用于C#。净静态类。这对我来说很奇怪,因为我现在在一个命名空间下有50个功能,做了一些谷歌,但没有找到任何东西,任何人都可以建议一个解决方案(除了更改
Module
到Class
)或任何要搜索的关键字?任何帮助将不胜感激!
I am having a "weird" situation of my VB.Net modules, as per my understanding,
Module
in VB.Net means static class so I have implemented a couple of helper modules with couple of functions each, let's have some examples for better explanation (free hand code, may contains syntax problem):Namespace Helpers Module HelperA Public Function FunctionA() As Boolean Return True End Function End Module End Namespace Namespace Helpers Module HelperB Public Function FunctionB() As Integer Return 1 End Function End Module End Namespace
When I start coding in Visual Studio and type
Helpers.
, bothFunctionA()
andFunctionB()
are show up in the recommended auto-complete dialog which I have not typeHelperA
orHelperB
yet, I have some C#.Net projects with static class and I found such behavior does not apply to C#.Net static class.It is weird to me and inconvenience since I am now having 50-ish functions under a single namespace, have done some Google but nothing could be find, could anyone suggest a solution (besides change
Module
toClass
) or any keywords to search with?Any help will be appreciate!
最满意答案
Module
在技术上并不意味着静态类。 VB.net中的静态(关于函数)是Shared
,并且没有Shared Class
。 我认为你想要的是一个带有静态/共享函数的密封/抽象/不可继承的类(你可以在没有父类实例的情况下调用函数,但是你仍然需要引用父类。调用函数)。 如果是这种情况,那么执行类似以下的操作:Public NotInheritable Class HelperA Public Shared Function FunctionA() as Boolean Return True End Function End Class
话虽如此,我在共享函数和模块函数之间找到的唯一区别 - 至少在实际应用中 - 可以在不引用模块的情况下调用模块函数。
Module
doesn't technically mean static class. Static in VB.net (with regard to functions) isShared
, and there is noShared Class
. What I think you want is a sealed/abstract/not-inheritable class with static/shared functions (you'll be able to call the functions without an instance of the parent class, but you'll still have to reference the parent class when calling the function). If that's the case, then do something similar to the following:Public NotInheritable Class HelperA Public Shared Function FunctionA() as Boolean Return True End Function End Class
Having said that, the only difference I've found—at least for practical purposes—between a shared function and a module function is that module functions can be called without referencing the module.
相关问答
更多-
asp.net (vb.net)怎样调用Module文件?[2019-05-30]
直接去添加引用~然后去调用就可以了 -
VB.NET中的类与模块(Classes vs. Modules in VB.NET)[2022-06-22]
Module是与C# static类的VB对应。 当您的类专为辅助函数和扩展方法设计,并且您不想允许继承和实例化时 ,您将使用Module 。 顺便说一句,使用Module不是很主观,并没有被弃用 。 确实,在适当的时候你必须使用Module 。 .NET Framework本身做了很多次(例如System.Linq.Enumerable )。 要声明扩展方法,需要使用Module s。 Modules are VB counterparts to C# static classes. When your ... -
对象装箱/ C#和VB.Net之间的引用差异(Difference in object boxing / comparing references between C# and VB.Net)[2023-01-30]
我相信这个问题的原因实际上是VB的Object处理,在某些地方,它比C#的dynamic更像一个普通的Object 。 具体而言,如果我将TrySetValue为: Public Function TrySetValue(oldValue As T, newValue As T) As Boolean Dim curValObj As Object = _value 'Threading.Volatile.Read(_value) Console.Write(Object.Referen ... -
VB.Net为什么这不是一个错误?(VB.Net why is this not a bug?)[2022-08-30]
在VB.NET 2008中,没有声明lambdas。 所有的lambda都是函数。 他们返回一个值,而不是执行一个动作。 你的VB lambda简单地比较d.PrivateBool和False并返回比较结果。 这不是一个错误和设计。 因此建议避免将VB.NET 2008的lambda分配给一个Action ,这对于一个毫无准备的人来说是非常混乱的。 声明lambdas在VB.NET 2010中出现。 In VB.NET 2008, there are no statement lambdas. All la ... -
它们被称为Class文件。 右键单击要添加类的解决方案或文件夹,然后选择“ Add new item然后从窗口中选择“ Class 。 They are called Class files. Right-click on your solution, or folder you wish to add the class and choose Add new item then from the window choose Class.
-
为什么它不允许你编译代码? thisShouldBeSet = True是一个有效的比较,返回值False (因为thisShouldBeSet <> True )。 请记住,在VB中, =可以表示C#中的= (赋值)和== (比较),具体取决于上下文。 详细说明, Sub() thisShouldBeSet = True将是一个简写 Sub Anonymous() thisShouldBeSet = True ' Assignment End Sub 而Function() ...
-
“Main()”是一个功能。 并且您声明了一个局部变量“sConnection”。 您的表格是另一个类。 类只能访问其成员,全局成员或全局静态成员(或某些朋友方案,如C ++)。 从“Main”中取出声明,在表单范围内声明或将其声明为全局变量,表单可以访问。 或者将您的连接字符串放在配置文件中并从中读取。 (在以后的时间点很容易配置。) The "Main()" is a function. And you declared a local variable "sConnection". Your Form ...
-
在VB.NET中直接调用模块函数是一种好习惯吗?(Is it good practice to call module functions directly in VB.NET?)[2021-08-17]
Intellisense(和“Goto定义”)应该能够轻松找到事物的位置,但我总是在调用时使用更好的命名空间,只是为了清晰阅读。 然后很明显它是一个自定义函数,而不是你正在使用的类内置或本地内容。 也许我缺少一个细微的差别,但我倾向于使用共享类而不是模块来代替常见和自包含的任何代码 - 它似乎更容易跟踪我,它也会强制执行你的前置规则它,因为你不能从任何地方调用它而不给出命名空间来调用它。 Intellisense (and "Goto Definition") should make it trivial ... -
VB.Net模块行为(VB.Net module behavior)[2022-06-25]
Module在技术上并不意味着静态类。 VB.net中的静态(关于函数)是Shared ,并且没有Shared Class 。 我认为你想要的是一个带有静态/共享函数的密封/抽象/不可继承的类(你可以在没有父类实例的情况下调用函数,但是你仍然需要引用父类。调用函数)。 如果是这种情况,那么执行类似以下的操作: Public NotInheritable Class HelperA Public Shared Function FunctionA() as Boolean Return ... -
将VB.net模块转换为C#(convert VB.net Module to C#)[2023-03-05]
您可以将常量放在public static class如下所示: public static class MyConnectionStringConstants { public const string strDatabase = "****"; public const string strUserID = "****"; public const string strPssWd = "****"; } 要使用它,您需要像这样引用常量: string strAccessConn ...