Using Components in ASP.NET (C#)

A

acb

Hi,

I wrote a DLL Component (using Visual Studio 2005) and managed to
include it into a C# Console application.

I am now trying to include this component into a Web project. I copy
the DLL into the bin directory but am not able to progress. Can anyone
please guide me to an online tutorial on the subject.

Thanks,
ACB
 
H

Henry Sun

acb said:
hi there,

I think you also need to add it to your project's references. You can
find the "references" in your solution panel at the top right hand side.
Right click the item and select add reference, then locate your dll file
and click ok.

Hope that helps
 
A

acb

Mark and Henry,

Sorry for not explaining myself properly. Thanks to you I have had a
better look at the code and the problem was not web vs console, it was
how to properly tell the compiler that it should use the newly created
namespace. I now have functional code but I haven't yet figured one
variation. This is my situation

I have the following Class Library:

using System;

namespace SimpleComponent
{
public class SimpleComponent
{
public string SayWelcomeStatement()
{
return "Hello World";
}
}
}

I compiled it into a release DLL.

I then created a console application solution called
TextSimpleComponent. I included a reference to the DLL created above
and in Program.cs included the following. It works.

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SimpleComponent.SimpleComponent scMessage = new
SimpleComponent.SimpleComponent();

Console.WriteLine (scMessage.SayWelcomeStatement());
Console.ReadKey();
}
}
}

On the other hand, the code below does not compile

using System;
using System.Collections.Generic;
using System.Text;
using SimpleComponent; // this is different

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
SimpleComponent scMessage = new SimpleComponent(); // This
is different

Console.WriteLine (scMessage.SayWelcomeStatement());
Console.ReadKey();
}
}
}

It returns the error: "'SimpleComponent' is a 'namespace' but is used
like a 'type'

Again thank you for your help.
 
G

Garfield

Yo acb,

I am not exactly sure, but why not try a different name for your class
name other than the same name of your namespace? That should at least
clarify where the error is comming from, the namespace or the class :)

Hope that helps
 
M

Mark Rae

I am not exactly sure, but why not try a different name for your class
name other than the same name of your namespace? That should at least
clarify where the error is comming from, the namespace or the class :)

That's just common sense, surely...? I can't imagine naming a class the same
as the namespace containing it.
 
A

acb

Mark Rae said:
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>&nbsp;<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>&nbsp;<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();

}
}
 
A

acb

Hi,

While assembling my previous I made a muck up in the source behind
Website CallNsEqCl. I am reposting the source in its entirety below. I
would like to point out that the line "using NsEqCl;" may be removed
when using the Namespace.Class referencing.

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>&nbsp;<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;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// This code does not work
//NsEqCl NEMessage1 = new NsEqCl();
//lblMessage1.Text = NEMessage1.SayHello();

// This code works
NsEqCl.NsEqCl NEMessage2 = new NsEqCl.NsEqCl();
lblMessage2.Text = NEMessage2.SayHello();

}
}

So unlike what I observed originally, when namespace = class name it
seems that one has to use the namespace.class notation.

Regards
ACB
 
M

Mark Rae

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.

http://www.amazon.com/gp/product/07...0?v=glance&n=283155&n=507846&s=books&v=glance

Take a look at some of the reviews, specifically those which comment on the
errors in the code examples.
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.

So now you know for yourself that the book you were using leaves a lot to be
desired... :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top