An Introduction to the .NET FCL, Part 3
Pages: 1, 2, 3, 4, 5
Example 1-6. Classes with the same name
Option Strict On
Namespace Extensions.Classes
Public Class Person
Dim sName As String
Public Sub New(Name As String)
sName = Name
End Sub
Public Property Name() As String
Get
Return sName
End Get
Set
sName = Value
End Set
End Property
End Class
End Namespace
Namespace Extensions.Demographics
Public Enum Gender As Short
Male = 2
Female = 1
End Enum
Public Class Person
Dim shAge As Short
Dim chGender As Gender
Public Property Age() As Short
Get
Return shAge
End Get
Set
shAge = Value
End Set
End Property
Public Property Gender() As Gender
Get
Return chGender
End Get
Set
chGender = Value
End Set
End Property
End Class
End Namespace
This code can be compiled into a dynamic link library. We can then attempt to access the Person class using code like that in Example 1-7.
Example 1-7. A type collision
Option Strict On
Imports Extensions.Classes
Imports Extensions.Demographics
Imports System
Module modMain
Public Sub Main()
Dim oPerson As New Person("John Doe")
Console.WriteLine(oPerson.Name)
Dim oPerson2 As New Person
oPerson2.Age = 32
oPerson2.Gender = Gender.Female
Console.WriteLine(oPerson2.Gender.ToString)
End Sub
End Module
However, when we attempt to compile this code, the VB.NET command-line compiler raises two instances of the following compiler error:
error BC30561: 'Person' is ambiguous, imported from the namespaces or types 'Extensions.Demographics, Extensions.Classes'.
To resolve this problem of type collisions, two solutions are available. The first is to use the fully qualified namespace name to indicate the namespace containing the type we want to instantiate, just as if we hadn't used the Imports statement. The second is to assign an alias to a namespace and to use that alias to identify the namespace containing the type we want to instantiate. To do this, we also use the Imports directive, which then has the following syntax:
Imports aliasname = namespace
where aliasname is the alias by which the namespace will be referenced in code, and namespace is the fully qualified namespace name.
We can then modify our code example from Example 1-7 to take advantage of aliasing. The result is shown in Example 1-8 (again, modified lines are shown in boldface).
Example 1-8. Using aliasing to prevent type naming conflicts
Option Strict On
Imports cl = Extensions.Classes
Imports Extensions.Demographics
Imports System
Module modMain
Public Sub Main()
Dim oPerson As New cl.Person("John Doe")
Console.WriteLine(oPerson.Name)
Dim oPerson2 As New Person
oPerson2.Age = 32
oPerson2.Gender = Gender.Female
Console.WriteLine(oPerson2.Gender.ToString)
End Sub
End Module
Note that we have aliased a single namespace, which has magically resolved the ambiguous reference to both namespaces. The use of an alias, however, means that all further references to types in the Extensions.Classes namespace must use the alias in order to resolve the reference to the type, or we must use the fully qualified name of the type.
In the next installment, learn the types of a .NET Namespaces
|
Related Reading VB.NET Core Classes in a Nutshell |


