Why Are Namespaces So Useful?

The purpose of namespaces is to enable a better organization of types. Regarding this, there are situations in which an object’s hierarchy could expose two different types with different behaviors but with the same name. For example, imagine you have two Person classes; the first one should represent a business contact, and the second one should represent your friends. Of course, you cannot create two classes with the same name within one namespace. Because of this, you can organize such types in different namespaces avoiding conflicts. The code in Listing 9.2 shows how you can define two Person classes within two different namespaces.

Listing 9.2 Avoiding Conflicts with Different Namespaces

image

This is the way how two classes with the same name can coexist within the same assembly. To access both of them you just need to invoke their full name as follows:

Dim aFriend As New MyFriends.Person
Dim aContact As New People.Person

Note

The Person class is here just an example. You can recur to inheritance as in Listing 9.1 instead of providing different namespaces, but the Person class is the simplest example possible for demonstrating topics, which is the reason to continue to use such a class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.27.58