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.Drawing
Sys

If you add an Imports statement for both namespaces (a reference may also be necessary for the project properties) ...

如果为两个名称空间都添加Imports语句(项目属性也可能需要引用)...


Imports System.Drawing
Imports 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 MyColor
Public Class Color
Sub Color()
' Do something
End Sub
End 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):

  1. Click Here to display the illustration

  2. Click 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:

  1. Click Here to display the illustration

  2. Click 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 MyColor
Public Class Color
Sub Color()
' Do something
End Sub
End 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):

  1. Click Here to display the illustration

  2. Click 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:

  1. Click Here to display the illustration

  2. Click 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的首席软件架构师,那么您可能想要组织名称空间,例如...


DRNo
Consulting
ReadTheirWatchNChargeEm
TellEmNuthin
Surgery
ElephantMan

This is similar to .NET's organization ...


DRNo
Consulting
ReadTheirWatchNChargeEm
TellEmNuthin
Surgery
ElephantMan

This is similar to .NET's organization ...

翻译自: https://www.thoughtco.com/namespaces-in-vbnet-3424445

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值