A class in .Net Framework cannot belong to multiple Namespaces. One class should belong to only one Namespace. VB.NET does not allow two classes with the same name to be used in a program.
Are the above statement correct? If yes then why? Why a class can't be a part of different namespace?
Loading
Deepak VermaPosted Mar 9, 2011, 3:33 AM
Classes in different namespaces can have similar names. But you should not import the namespaces, otherwise it will show error like :
'class1' is ambiguous, imported from the namespaces or types 'ConsoleApplication1.B, ConsoleApplication1.A'.
where,
ConsoleApplication1 - application name
A & B - name of namespaces
class1 - name of class
I hope following Code (containing error) will help you to understand the topic :
* Follow the comments in program to get errors free code
Imports System
Imports ConsoleApplication1.A 'Dont use this code
Imports ConsoleApplication1.B 'Dont use this code
'Use "Imports ConsoleApplication1"
Namespace A
Class class1
Public Shared Sub Show()
Console.WriteLine("Namespace A")
End Sub
End Class
EndNamespace
Namespace B
Class class1
Public Shared Sub Show()
Console.WriteLine("Namespace B")
End Sub
End Class
End Namespace
Module Module1
Sub Main()
class1.Show() 'Use "A.class1.Show() for Namespace A"
class1.Show() 'Use "B.class2.show() for Namespace B"
Console.Read()
End Sub
EndModule
Note: you can also use Full path to each sub/function as "ConsoleApplication1.A.class1.show()" to call show() without importing the Application. It'll work also.
Rohatash KumarPosted Mar 9, 2011, 3:13 AM
A class can be the part of different namespace. we take two namespace and one class name belong both namespace.
The below code define it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace mukhi
{
class zzz
{
public static void abc()
{
System.Console.WriteLine("abc in zzz ");
}
static void Main()
{
vijay.zzz.abc();
abc();
}
}
}
namespace vijay
{
class zzz
{
public static void abc()
{
System.Console.WriteLine("abc");
}
}
}
Hope it will help u and give you right direction.
Thanks,
Rohatash kumar