是否可以在类中获得命名空间的功能?
我有以下代码:
Public Class Form1
Private Function Step1_Execute() As Boolean
Return Step1_Verify()
End Function
Private Function Step1_Verify() As Boolean
Return True
End Function
Private Function Step2_Execute() As Boolean
Return Step2_Verify()
End Function
Private Function Step2_Verify() As Boolean
Return True
End Function
End Class
我想做的是能够分离代码,类似于以下内容(这显然不起作用):
Public Class Form1
Namespace Step1
Private Function Step1_Execute() As Boolean
Return Step1_Verify()
End Function
Private Function Step1_Verify() As Boolean
Return True
End Function
End Namespace
Namespace Step2
Private Function Step2_Execute() As Boolean
Return Step2_Verify()
End Function
Private Function Step2_Verify() As Boolean
Return True
End Function
End Namespace
End Class
我想要类内部的命名空间的功能,就像在某些东西中一样让我调用 Step2.Execute() 而不必将 Step2_ 放在一大堆函数前面。我不想为步骤 1、步骤 2 等创建单独的类/模块。
有没有一种方法可以从类内部完成命名空间功能?
I have the following code:
Public Class Form1
Private Function Step1_Execute() As Boolean
Return Step1_Verify()
End Function
Private Function Step1_Verify() As Boolean
Return True
End Function
Private Function Step2_Execute() As Boolean
Return Step2_Verify()
End Function
Private Function Step2_Verify() As Boolean
Return True
End Function
End Class
What I would like to do is be able to separate the code, similar to the following (which obviously doesn't work):
Public Class Form1
Namespace Step1
Private Function Step1_Execute() As Boolean
Return Step1_Verify()
End Function
Private Function Step1_Verify() As Boolean
Return True
End Function
End Namespace
Namespace Step2
Private Function Step2_Execute() As Boolean
Return Step2_Verify()
End Function
Private Function Step2_Verify() As Boolean
Return True
End Function
End Namespace
End Class
I would like the functionality of namespaces inside of a class, as in something that would let me call Step2.Execute() instead of having to put Step2_ in front of a whole bunch of functions. I don't want to have to create separate classes / modules for step1, step2, etc.
Is there a way that I can accomplish namespace functionality from inside a class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只使用 模块 怎么样(大部分相当于 C# 中的静态类)?这似乎可以很好地完成工作,并为您提供您想要的参考样式。
我相信,如果您愿意,您甚至可以将 VB.NET
Module
嵌套在其他类中。编辑:
刚刚意识到所有模块都有相同的契约(方法签名集),因此在这里实现一个接口并创建该类的实例实际上是有意义的。也许需要更多的代码,但如果您想抽象化事物,那么可能值得付出努力。
How about just using a module (equivalent for the most part to a static class in C#)? That would seem to do the job well and give you the reference style you want.
I believe you can even nest VB.NET
Module
s within other classes, if you wish.Edit:
Just realised that all of the modules have the same contract (set of method signatures), so it would actually make sense to implement an interface here, and create instances of the class. A tad more code perhaps, but if you want to abstractify things, it may be worth the effort.
不。
命名空间并不真实存在。命名空间只是类名称的一部分。因此,如果您有 System.Text.StringBuilder 类,则“using System.Text”部分是对编译器的提示,例如“如果找不到该类型,请查找具有以“System”开头的未知标识符的所有类型未知部分前面的.Text'。
No.
Namespaces are nothing real existing. A namespace is merely a part of the name of a class. So if you have the class System.Text.StringBuilder, the "using System.Text" part is a hint to the compiler like "if you don't find the type, look for all types with the unknown identifier that start with 'System.Text' in front of the unknown part.
显然你需要将其分为 3 类。一个将执行代码的人。比 2 继承了相同的接口,其中包含执行和验证方法。
然后,执行类将包含该接口的集合,您只需按照您想要执行的顺序添加它们即可。
Obviously you need to split this up in 3 classes. One that will execute the code. And than 2 that inherit the same interface with an execute and verify method in it.
The executing class wil then contain a collection of this interface and you just add them in the order you wan to execute them.