-
vb.net语句_VB.NET导入语句与引用
The actual effect of the Imports statement in VB.NET is often a source of confusion for people learning the language. And the interaction with VB.NET References makes for even more confusion. We're going to clear that up in this Quick Tip.
VB.NET中Imports语句的实际效果通常会使学习该语言的人感到困惑。 与VB.NET References的交互会带来更多混乱。 我们将在此快速提示中对此进行清理。
Here's a brief summary of the whole story. Then we'll go over the details.
这是整个故事的简短摘要。 然后,我们将详细介绍。
A Reference to a VB.NET namespace is a requirement and must be added to a project before the objects in the namespace can be used. (A set of references is automatically added for the different templates in Visual Studio or VB.NET Express. Click "Show All Files" in Solution Explorer to see what they are.) But the Imports statement is not a requirement. Instead, it's simply a coding convenience that allows shorter names to be used.
对VB.NET命名空间的引用是一项要求,必须先将其添加到项目中,然后才能使用该命名空间中的对象。 (将为Visual Studio或VB.NET Express中的不同模板自动添加一组引用。在解决方案资源管理器中单击“显示所有文件”以查看它们的含义。)但是,Imports语句不是必需的。 相反,它只是一种编码便利,允许使用较短的名称。
Now let's look at an actual example. To illustrate this idea, we're going to use the System.Data namespace — which provides ADO.NET data technology.
现在让我们看一个实际的例子。 为了说明这个想法,我们将使用System.Data命名空间-它提供ADO.NET数据技术。
System.Data is added to Windows applications as a Reference by default using the VB.NET Windows Forms Application template.
默认情况下,使用VB.NET Windows窗体应用程序模板将System.Data作为参考添加到Windows应用程序中。
在引用集合中添加名称空间 ( Adding a Namespace in the References Collection )
Adding a new namespace to the References collection in a project makes the objects in that namespace available to the project as well. The most visible effect of this is that the Visual Studio "Intellisense" will help you find the objects in popup menu boxes.
在项目中的References集合中添加新的名称空间会使该名称空间中的对象也可用于项目。 最明显的效果是Visual Studio“ Intellisense”将帮助您在弹出菜单框中找到对象。
If you attempt to use an object in your program without a Reference, the line of code generates an error.
如果尝试在程序中使用没有引用的对象,则代码行将产生错误。
The Imports statement, on the other hand, is never required. The only thing it does is allow the name to be resolved without being fully qualified. In other words (emphasis added to show the differences).
另一方面,从不需要Imports语句。 唯一要做的是允许在不完全限定的情况下解析名称。 换句话说(加重以显示差异)。
Imports System.Data
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Form1_Load( ...
Dim Test As OleDb.OleDbCommand
End Sub
End Class
and
和
Imports System.Data.OleDb
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Form1_Load( ...
Dim Test As OleDbCommand
End Sub
End Class
are both equivalent. But ...
都是等效的。 但是...
Imports System.Data
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub Form1_Load( ...
Dim Test As OleDbCommand
End Sub
End Class
results in a syntax error ("Type 'OleDbCommand' is not defined") because of the Imports namespace qualification System.Data doesn't provide enough information to find the object OleDbCommand.
导致语法错误(“类型'OleDbCommand'未定义”),原因是Imports命名空间限定System.Data没有提供足够的信息来查找对象OleDbCommand。
Although the qualification of names in your program source code can be coordinated at any level in the 'apparent' hierarchy, you still have to pick the right namespace to reference. For example, .NET provides a System.Web namespace and a whole list of others starting with System.Web ...
尽管可以在“表观”层次结构中的任何级别上协调程序源代码中名称的限定,但仍必须选择正确的命名空间进行引用。 例如,.NET提供了System.Web命名空间以及以System.Web开头的其他列表。
注意 ( Note )
There are two entirely different DLL files for the references. You DO have to pick the right one because WebService isn't a method in one of them.
有两个完全不同的DLL文件供参考。 您必须选择正确的方法,因为WebService不是其中一种方法。