VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • VB.NET中的命名空间

The most common way VB.NET namespaces are used by most programmers is to tell the compiler which .NET Framework libraries are needed for a particular program. When you choose a "template" for your project (such as "Windows Forms Application") one of the things that you're choosing is the specific set of namespaces that will be automatically referenced in your project. This makes the code in those namespaces available to your program.
 
大多数程序员使用VB.NET命名空间最常见的方式是告诉编译器特定程序需要哪些.NET Framework库。 当为项目(例如“ Windows Forms Application”)选择“模板”时,要选择的一件事就是将在项目中自动引用的特定名称空间集。 这使那些名称空间中的代码可用于您的程序。
 
For example, some of the namespaces and the actual files they are in for a Windows Forms Application are:
 
例如,Windows窗体应用程序中的某些名称空间和实际文件为:
 
System > in System.dllSystem.Data > in System.Data.dllSystem.Deployment > System.Deployment.dllSystem.Drawing > System.Drawing.dllSystem.Windows.Forms > System.Windows.Forms.dll
 
系统>在System.dllSystem.Data中>在System.Data.dllSystem.Deployment中> System.Deployment.dllSystem.Drawing> System.Drawing.dllSystem.Windows.Forms> System.Windows.Forms.dll
 
You can see (and change) the namespaces and references for your project in the project properties under the References tab.
 
您可以在“ 引用”选项卡下的项目属性中查看(并更改)项目的名称空间和引用。
 
This way of thinking about namespaces makes them seem to be just the same thing as "code library" but that's only part of the idea. The real benefit of namespaces is organization.
 
这种关于名称空间的思考方式使它们看起来与“代码库”一样,但这只是其中的一部分。 名称空间的真正好处是组织。
 
Most of us won't get the chance to establish a new namespace hierarchy because it's generally only done once 'in the beginning' for a large and complicated code library. But, here, you'll learn how to interpret the namespaces that you will be asked to use in many organizations.
 
我们大多数人没有机会建立新的名称空间层次结构,因为对于大型而复杂的代码库,它通常仅在“开始”时完成一次。 但是,在这里,您将学习如何解释将要求您在许多组织中使用的名称空间。
 
命名空间做什么 ( What Namespaces Do )
Namespaces make it possible to organize the tens of thousands of .NET Framework objects and all the objects that VB programmers create in projects, too, so they don't clash.
 
命名空间使组织成千上万的.NET Framework对象以及VB程序员在项目中创建的所有对象成为可能,因此它们不会冲突。
 
For example, if you search .NET for a Color object, you find two. There is a Color object in both:
 
例如,如果在.NET中搜索Color对象,则会找到两个。 两者中都有一个Color对象:
 
 
System.DrawingSys
 
If you add an Imports statement for both namespaces (a reference may also be necessary for the project properties) ...
 
如果为两个名称空间都添加Imports语句(项目属性也可能需要引用)...
 
 
Imports System.DrawingImports Sys
 
... then a statement like ...
 
...然后像...这样的声明
 
... will be flagged as an error with the note, "Color is ambiguous" and .NET will point out that both namespaces contain an object with that name. This kind of error is called a "name collision."
 
...将被标记为带有错误的错误标记,“颜色不明确”,.NET将指出两个名称空间都包含具有该名称的对象。 这种错误称为“名称冲突”。
 
This is the real reason for "namespaces" and it's also the way namespaces are used in other technologies (such as XML). Namespaces make it possible to use the same object name, such as Color, when the name fits and still keep things organized. You could define a Color object in your own code and keep it distinct from the ones in .NET (or the code of other programmers).
 
这是“命名空间”的真正原因,也是其他技术(例如XML)中使用命名空间的方式。 使用名称空间,可以在名称合适的情况下使用相同的对象名称,例如Color ,并且仍然使事物保持井井有条。 您可以在自己的代码中定义Color对象,并使其与.NET(或其他程序员的代码)中的对象不同。
 
 
Namespace MyColorPublic Class ColorSub Color()' Do somethingEnd SubEnd Class
 
You can also use the Color object somewhere else in your program like this:
 
 
Before getting into some of the other features, be aware that every project is contained in a namespace. VB.NET uses the name of your project (WindowsApplication1 for a standard forms application if you don't change it) as the default namespace. To see this, create a new project (we used the name NSProj and check out the Object Browser tool):
 
 
   Click Here to display the illustrationClick the Back button on your browser to return
 
The Object Browser shows your new project namespace (and the automatically defined objects in it) right along with the .NET Framework namespaces. This ability of VB.NET to make your objects equal to .NET objects is one of the keys to the power and flexibility. For example, this is why Intellisense will show your own objects as soon as you define them.
 
 
To kick it up a notch, let's define a new project (We named ours NewNSProj in the same solution (use File > Add > New Project ...) and code a new namespace in that project. And just to make it more fun, let's put the new namespace in a new module (we named it NewNSMod). And since an object must be coded as a class, we also added a class block (named NewNSObj). Here's the code and Solution Explorer to show how it fits together:
 
 
   Click Here to display the illustrationClick the Back button on your browser to return
 
Since your own code is 'just like Framework code', it's necessary to add a reference to NewNSMod in NSProj to use the object in the namespace, even though they're in the same solution. Once that's done, you can declare an object in NSProj based on the method in NewNSMod. You also need to "build" the project so an actual object exists to reference.
 
Dim o As New NewNSProj.AVBNS.NewNSMod.NewNSObj
That's quite a Dim statement though. We can shorten that by using an Imports statement with an alias.
 
 
Namespace MyColorPublic Class ColorSub Color()' Do somethingEnd SubEnd Class 
 
You can also use the Color object somewhere else in your program like this:
 
 
Before getting into some of the other features, be aware that every project is contained in a namespace. VB.NET uses the name of your project ( WindowsApplication1 for a standard forms application if you don't change it) as the default namespace. To see this, create a new project (we used the name NSProj and check out the Object Browser tool):
 
 
   Click Here to display the illustrationClick the Back button on your browser to return
 
The Object Browser shows your new project namespace (and the automatically defined objects in it) right along with the .NET Framework namespaces. This ability of VB.NET to make your objects equal to .NET objects is one of the keys to the power and flexibility. For example, this is why Intellisense will show your own objects as soon as you define them.
 
 
To kick it up a notch, let's define a new project (We named ours NewNSProj in the same solution (use File > Add > New Project ... ) and code a new namespace in that project. And just to make it more fun, let's put the new namespace in a new module (we named it NewNSMod ). And since an object must be coded as a class, we also added a class block (named NewNSObj ). Here's the code and Solution Explorer to show how it fits together:
 
 
   Click Here to display the illustrationClick the Back button on your browser to return
 
Since your own code is 'just like Framework code', it's necessary to add a reference to NewNSMod in NSProj to use the object in the namespace, even though they're in the same solution. Once that's done, you can declare an object in NSProj based on the method in NewNSMod . You also need to "build" the project so an actual object exists to reference.
 
Dim o As New NewNSProj.AVBNS.NewNSMod.NewNSObj
That's quite a Dim statement though. We can shorten that by using an Imports statement with an alias.
 
 
A requirement for namespace organization in the first place. You need more than just a "Hello World" project before the organization of namespaces starts to pay off.
首先是名称空间组织的要求。 在命名空间的组织开始获得回报之前,您不仅仅需要一个“ Hello World”项目。
A plan to use them.
使用它们的计划。
In general, Microsoft recommends that you organize your organization's code using a combination of your company name with the product name.
 
通常, Microsoft建议您使用公司名称和产品名称的组合来组织组织的代码。
 
So, for example, if you're the Chief Software Architect for Dr. No's Nose Knows Plastic Surgery, then you might want to organize your namespaces like ...
 
因此,例如,如果您是No's Nose Knows Surgery的首席软件架构师,那么您可能想要组织名称空间,例如...
 
 
DRNoConsultingReadTheirWatchNChargeEmTellEmNuthinSurgeryElephantMan
 
This is similar to .NET's organization ...
 
 
DRNoConsultingReadTheirWatchNChargeEmTellEmNuthinSurgeryElephantMan
 
This is similar to .NET's organization ...
 
 
翻译自: https://www.thoughtco.com/namespaces-in-vbnet-3424445

相关教程