首页 \ 问答 \ VB.Net模块行为(VB.Net module behavior)

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()都显示在推荐的自动完成对话框中,我还没有输入HelperAHelperB ,我有一些静态类的C#.Net项目,我发现这种行为不适用于C#。净静态类。

这对我来说很奇怪,因为我现在在一个命名空间下有50个功能,做了一些谷歌,但没有找到任何东西,任何人都可以建议一个解决方案(除了更改ModuleClass )或任何要搜索的关键字?

任何帮助将不胜感激!


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., both FunctionA() and FunctionB() are show up in the recommended auto-complete dialog which I have not type HelperA or HelperB 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 to Class) or any keywords to search with?

Any help will be appreciate!

更新时间:2022-06-25 15:06

最满意答案

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) is Shared, and there is no Shared 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.

相关问答

更多
  • 直接去添加引用~然后去调用就可以了
  • Module是与C# static类的VB对应。 当您的类专为辅助函数和扩展方法设计,并且您不想允许继承和实例化时 ,您将使用Module 。 顺便说一句,使用Module不是很主观,并没有被弃用 。 确实,在适当的时候你必须使用Module 。 .NET Framework本身做了很多次(例如System.Linq.Enumerable )。 要声明扩展方法,需要使用Module s。 Modules are VB counterparts to C# static classes. When your ...
  • 我相信这个问题的原因实际上是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 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 ...
  • Intellisense(和“Goto定义”)应该能够轻松找到事物的位置,但我总是在调用时使用更好的命名空间,只是为了清晰阅读。 然后很明显它是一个自定义函数,而不是你正在使用的类内置或本地内容。 也许我缺少一个细微的差别,但我倾向于使用共享类而不是模块来代替常见和自包含的任何代码 - 它似乎更容易跟踪我,它也会强制执行你的前置规则它,因为你不能从任何地方调用它而不给出命名空间来调用它。 Intellisense (and "Goto Definition") should make it trivial ...
  • Module在技术上并不意味着静态类。 VB.net中的静态(关于函数)是Shared ,并且没有Shared Class 。 我认为你想要的是一个带有静态/共享函数的密封/抽象/不可继承的类(你可以在没有父类实例的情况下调用函数,但是你仍然需要引用父类。调用函数)。 如果是这种情况,那么执行类似以下的操作: Public NotInheritable Class HelperA Public Shared Function FunctionA() as Boolean Return ...
  • 您可以将常量放在public static class如下所示: public static class MyConnectionStringConstants { public const string strDatabase = "****"; public const string strUserID = "****"; public const string strPssWd = "****"; } 要使用它,您需要像这样引用常量: string strAccessConn ...

相关文章

更多

最新问答

更多
  • 在csproj中使用appdata环境变量(Use appdata environment variable in csproj)
  • 从背景返回后,Skobbler Map崩溃(Skobbler Map crashes after returning from background)
  • 如何保持对绑定服务的轮询?(How to keep polling a bound service?)
  • ASP.NET单选按钮jQuery处理(ASP.NET radio button jQuery handling)
  • Linux上的FORTRAN图形库(FORTRAN graphic library on Linux)
  • 我们如何根据索引更新dynamodb表(不基于primary has和range key)(how can we update dynamodb table based on index(not based on primary has and range key))
  • 功能包装避免重复(wrap of functions avoid duplicating)
  • Android BroadcastReceiver和Activity.onPause()(Android BroadcastReceiver and Activity.onPause())
  • 无法使用phonegap 2.4在Android上播放录音(unable to play audio recordings on android using phonegap 2.4)
  • VS2015 + Resharper:不要使用C#6(VS2015 + Resharper: Don't use C#6)
  • 大学电脑四级对初学者来说要多久能过
  • 特殊字符删除?(Special characters remove?)
  • Android视频教程现在网上的都比较零散呢?有些太坑爹了,感觉老师就是在想当然的讲
  • 计算同一个表中不同行之间的差异[重复](Calculate delta's between different rows in same table [duplicate])
  • Javaweb开发,技术路线是什么?该怎么写?
  • JavaScript只在php代码中执行一次(JavaScript only executes once inside php code)
  • 不兼容的字符编码:ASCII-8BIT和UTF-8(incompatible character encodings: ASCII-8BIT and UTF-8)
  • Clojure(加载文件)给出错误(Clojure (load-file) gives an error)
  • 为具有瞬态scala依赖性的spring-xd项目优化gradle(Optimize gradle for spring-xd project with transient scala dependency)
  • 如何才能在Alpha测试模式下发布我的应用程序?(How can I publish my app in Alpha test mode only?)
  • “没有为此目标安装系统映像”Xamarin AVD Manager(“No system images installed for this target” Xamarin AVD Manager)
  • maven中的Scalatest:JUnit结果(Scalatest in maven: JUnit results)
  • 使用android SDK将文件直接上传到存储桶中的文件夹(Upload a file directly to a folder in bucket using android SDK)
  • 是否应将plists导入CoreData?(Should plists be imported to CoreData?)
  • java.lang.reflect.InvocationTargetException JavaFX TableView(java.lang.reflect.InvocationTargetException JavaFX TableView)
  • 根据唯一列值动态创建多个子集(Dynamically create multiple subsets based on unique column values)
  • 使用CSS可以使HTML锚标签不可点击/可链接吗?(Is it possible to make an HTML anchor tag not clickable/linkable using CSS?)
  • 嵌套的模板可能性(Nested template possibilities)
  • 任何方式在iOS7 +上以编程方式打开蓝牙(Any way to turn on bluetooth programmatically on iOS7+)
  • 如何为给定的SQL查询编写JPA查询(How I can write JPA query for given SQL query)