You might not think much about namespaces now, but when you start creating large applications or start to reuse your code from other projects, namespace will become an issue. So before you just blow off the namespace description, think hard about what it really means and then give it an appropriate name. Let’s look at an example of where you might run into problems.
When you do run into namespace problems, the first thing you will probably see is the error message shown in Figure 10.3.
As the message indicates, there is an ambiguous reference. In other words, there are two classes called Class1 and the compiler does not know which to use.
This is where namespaces come into play. First, you’ll take a look at how we arrived at this point; then you’ll see what you need to do to fix the problem. If you want to use these snippets of code, you need to start with an existing web project or windows application. Then you can add the following snippets to your project to see how they react. Listings 10.8– 10.11 illustrate the problem and show how namespaces enter into its solution.
namespace MySpace { public class Class1 { public Class1() { } public string[] GetMyList() { string[] mylist = new string[4]; mylist[0]="cars"; mylist[1]="trucks"; mylist[2]="planes"; mylist[3]="boats"; return mylist; } } } |
Namespace MySpace Public Class Class1 Public Function GetMyList() As String() Dim mylist As String() mylist(0) = "cars" mylist(1) = "trucks" mylist(2) = "planes" mylist(3) = "boats" Return mylist End Function End Class End Namespace |
namespace YourSpace { public class Class1 { public Class1() { } public string[] GetMyList() { string[] mylist = new string[4]; mylist[0]="eggs"; mylist[1]="bacon"; mylist[2]="milk"; mylist[3]="bread"; return mylist; } } } |
Namespace YourSpace Public Class Class1 Public Function GetMyList() As String() Dim mylist As String() mylist(0) = "cars" mylist(1) = "trucks" mylist(2) = "planes" mylist(3) = "boats" Return mylist End Function End Class End Namespace |
As you can see from these listings, both classes have the same name and the same methods. The only thing that differentiates them is their namespace. Now take a look at how we were trying to implement these classes in the web page (see Listing 10.12).
<%@ Import Namespace="MySpace" %> <%@ Import Namespace="YourSpace" %> <script language="C#" runat=server> private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here Class1 c1 = new Class1();//Ambiguous Reference ListBox1.DataSource = c1.GetMyList(); ListBox1.DataSource = c1.GetMyList(); ListBox1.DataBind(); ListBox2.DataBind(); } </script> <HTML> <HEAD> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="WebForm3" method="post" runat="server"> <asp:ListBox id=ListBox1 runat="server"></asp:ListBox> <asp:ListBox id=ListBox2 runat="server"></asp:ListBox> </form> </body> </body> </HTML> |
As you can see, the namespaces have been included at the beginning of the file so that the compiler can identify the classes. What you need to do is preface the class with the namespace that it belongs to; then the compiler knows definitively what you want to do . Take a look at the code in Listing 10.13 to see the correct implementation for these classes.
<%@ Import Namespace="MySpace" %> <%@ Import Namespace="YourSpace" %> <script language="C#" runat=server> private void Page_Load(object sender, System.EventArgs e) { MySpace.Class1 c1 = new MySpace.Class1(); YourSpace.Class1 c2 = new YourSpace.Class1(); ListBox1.DataSource = c1.GetMyList(); ListBox1.DataSource = c2.GetMyList(); ListBox1.DataBind(); ListBox2.DataBind(); } </script> |
As you can see here, you need to identify which namespace you are using if there are naming conflicts like those with Class1.
Next you take a look at XML bindings and some hurdles that you might run into.