"Mark Rae" <m...@mark-N-O-S-P-A-M-rae.co.uk> wrote
That's just common sense, surely...? I can't imagine naming a class the same
as the namespace containing it.
I'm too new on the subject to comment on that :-) My only observation
is that the book ASP.NET by Danny Ryan and Tommy Ryan (Hungry Minds
publishers) has examples written in a form in which the namespace and
the class have the same names.
If this is such a no-no I would suggest that an enhancement would be
say a warning by the compiler to that effect. I was impressed by the
fact that the compiler tells me that a variable I am using might not
have been properly initialised so I don't think that this should be
difficult to implement.
For the benefit of others who might encounter a problem similar to
mine, the problem ***was*** the fact the namespace and the class had
the same name.
The following code should help demonstrate the problem and reproduce
it:
------------------------------------------------------------------------------------------------
Class Library NsNeCl. Build release dll.
using System;
using System.Collections.Generic;
using System.Text;
/* In this example, the namespace has a different name to the class. In
my tests
* this combination works.
*
* ACB - Nov 2005
*/
namespace NsNeCl
{
public class Class1
{
public string SayHello()
{
return "Hello, World";
}
}
}
------------------------------------------------------------------------------------------------
Website CallNsNeCl. Include in this project the release dll produced in
NsNeCl.
Default.aspx code
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblMessage1"
runat="server"></asp:Label> <br />
<asp:Label ID="lblMessage2" runat="server"></asp:Label></div>
</form>
</body>
</html>
- - - - - - - - - - - - - - - - - - - - - -
File Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NsNeCl;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
/* Since the namespace in included in the class one can
automatically
* create new objects by referencing classes defined within it
*/
Class1 NEMessage1 = new Class1();
lblMessage1.Text = NEMessage1.SayHello();
/* The extended version of the code shown above. Here the
object is
* referenced using both namespace and class
*/
NsNeCl.Class1 NEMessage2 = new NsNeCl.Class1();
lblMessage2.Text = NEMessage2.SayHello();
}
}
This solution works.
------------------------------------------------------------------------------------------------
Class Library NsEqCl. Build release dll.
using System;
using System.Collections.Generic;
using System.Text;
/* In this example, the namespace carries the same name as the class.
In my tests
* it did not work
*
* ACB - Nov 2005
*/
namespace NsEqCl
{
public class NsEqCl
{
public string SayHello()
{
return "Hello, World";
}
}
}
------------------------------------------------------------------------------------------------
Website CallNsEqCl. Include in this project the release dll produced in
NsEqCl.
Default.aspx code
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblMessage1"
runat="server"></asp:Label> <br />
<asp:Label ID="lblMessage2" runat="server"></asp:Label></div>
</form>
</body>
</html>
- - - - - - - - - - - - - - - - - - - - - -
File Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NsEqCl;
// Intellisence identifies two methods in NsEqCl
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Error 1 The type or namespace name 'Class1' could not be
found (are you missing a using directive or an assembly reference?)
//Class1 NEMessage1 = new Class1();
//lblMessage1.Text = NEMessage1.SayHello();
// Error 1 The type or namespace name 'Class1' does not exist
in the namespace 'NsEqCl' (are you missing an assembly reference?)
//NsEqCl.Class1 NEMessage2 = new NsEqCl.Class1();
//lblMessage2.Text = NEMessage2.SayHello();
}
}