VB.NET 导入类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/243900/
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
VB.NET Importing Classes
提问by Kevin
I've seen some code where a Classis imported, instead of a namespace, making all the static members/methods of that class available. Is this a feature of VB? Or do other languages do this as well?
我看过一些代码,其中导入了一个类,而不是一个命名空间,使该类的所有静态成员/方法都可用。这是VB的特性吗?或者其他语言也这样做?
TestClass.vb
测试类.vb
public class TestClass
public shared function Somefunc() as Boolean
return true
end function
end class
MainClass.vb
主类.vb
imports TestClass
public class MainClass
public sub Main()
Somefunc()
end sub
end class
These files are in the App_Code directory. Just curious, because I've never thought of doing this before, nor have I read about it anywhere.
这些文件位于 App_Code 目录中。只是好奇,因为我以前从未想过这样做,也没有在任何地方读到过。
采纳答案by RS Conley
One of the reasons this feature is in place is to emulate Visual Basic 6.0's GlobalMultiUse Option for Instancing. Visual Basic 6.0 doesn't have the ability to make modules public across a DLL boundary. Instead you set the instancing property to GlobalMultiUse
. It is used mainly for utility classes like a class that export a series of math functions.
使用此功能的原因之一是模拟 Visual Basic 6.0 的 GlobalMultiUse Option for Instancing。Visual Basic 6.0 无法跨 DLL 边界公开模块。相反,您将实例化属性设置为GlobalMultiUse
. 它主要用于实用程序类,例如导出一系列数学函数的类。
Every time you call a subroutine or function on a class with the GlobalMultiUse Instancing
, Visual Basic 6.0 instantiates a class behind the scenes before calling the function.
每次使用 调用类上的子例程或函数时GlobalMultiUse Instancing
,Visual Basic 6.0 都会在调用函数之前在幕后实例化一个类。
It can be abused to generate global functions/variables with all the advantages and disadvantages.
可以滥用它来生成具有所有优点和缺点的全局函数/变量。
回答by Bob King
Yes, it's a Visual Basic language feature. While you can create aliases, using C#'s using statement, it doesn't appear that you can import a shared class into scope. To be honest, I've only ever used it once in a legacy project that had already used it. I see some value, but I'm afraid it may cause more harm than good for future maintainability of your code.
是的,它是Visual Basic 语言功能。虽然您可以使用C# 的 using 语句创建别名,但似乎无法将共享类导入作用域。老实说,我只在已经使用过它的遗留项目中使用过一次。我看到了一些价值,但我担心它可能对您的代码未来的可维护性造成弊大于利。
回答by Jonathan Allen
I use it whenever I am using the same library a lot of time. A good example is System.Math.
每当我经常使用同一个库时,我都会使用它。System.Math 就是一个很好的例子。
C# doesn't support this, which I find to be very annoying.
C# 不支持这个,我觉得这很烦人。
回答by thismat
Actually, that function is available because it's a sharedfunction. If you were to remove the shared modifier, you would still have to create an instance of the class to access it.
实际上,该功能可用,因为它是共享功能。如果您要删除 shared 修饰符,您仍然需要创建类的一个实例来访问它。
To achieve access to all variables and all functions within a class by default, you would want to inherit it.
要默认访问类中的所有变量和所有函数,您需要继承它。
To my knowledge importinga class is basically tying direct reference to it, not creating any sort of instance of it for you to use.
据我所知,导入一个类基本上是将直接引用绑定到它,而不是创建它的任何类型的实例供您使用。
EDIT for clarity: The links are there are VB specific links, thus, explaining the functionality of this pertaining to VB.NET
为清楚起见编辑:链接是特定于 VB 的链接,因此,解释了与 VB.NET 相关的功能
回答by gumuruh
wait, wait, wait....
等等,等等,等等……
I found just this morning that we could derived all the objects (class-s) inside any class that need their references using this method / function;
就在今天早上,我发现我们可以使用此方法/函数派生任何需要引用的类中的所有对象(类);
Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub