Why doesn't this simple code compile?

G

Guest

using Team Studio 2005
1. Create a new web site
2. Add a new class to the project
3. In the Page_Load handler of default.aspx.cxs, create a reference to the
new class
4. compile solution
Result: The type or namespace name 'TestClass' could not be found (are you
missing a using directive or an assembly reference?)

For example, here's my 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;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TestClass tc = new q();
}
}

here's the class I created:
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;

/// <summary>
/// Summary description for TestClass
/// </summary>
public class TestClass
{
public TestClass()
{
}
}

I duplicated the same behavior using VB.NET.

In Visual Studio 2003, the above code compiled, in 2005, it does not. Why
not?

Thanks.
 
R

Roland

Sorry Roy - I didn't read that carefully enough
For my opinion you have to write:
TestClass tc = new TestClass();

Your code:
TestClass tc = new q();
is not valid - how can the compiler know what this 'q()' is - even we
can not :)

Hope this helps
Roland
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Roy,

Make sure that the page class and your class are in the same namespace
 
T

tdavisjr

what is new q()? Is q a class. Maybe you meant new TestClass() instead
of new q() like Roland suggested.
 
G

Guest

A portion of the sample code was pasted incorrectly. The Page_Load method
should be:

protected void Page_Load(object sender, EventArgs e)
{
TestClass tc = new TestClass();
}
 
G

Guest

Which error message did you recieve on that code?

In file default.aspx.cs:

"The type or namespace name 'TestClass' could not be found (are you missing
a using directive or an assembly reference?)"
 
G

Guest

This must be a typo (I hope):
TestClass tc = new q();

new q()? What class is that? It's not evident from the context of your post.

Otherwise, have you tried adding the test class to your APP_CODE Folder?
Peter
 
G

Guest

Otherwise, have you tried adding the test class to your APP_CODE Folder?

Yes, I have...and it works great. The problem is that I don't want to place
the code in the APP_CODE folder. I want to place code where it logically
makes sense.

Has Microsoft restricted where I can place code?

Thanks!
 
G

Guest

"q" was a mistake. The actual code is:

TestClass tc = new TestClass();

But, try it for yourself. The error is easy to duplicate.
 
G

Guest

Make sure that the page class and your class are in the same namespace

That doesn't appear to matter. The error can be duplicated in less than a
minute.

Roy
 
G

Guest

q() was a mistake I made when posting. The actual code uses TestClass();

Please duplicate the error - it takes less than a minute. It seems that MS
has created restrictions where code can be placed.

Thanks.

Roy
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Even though new q() looks fishy, reported bug is not related to it. The
compiler doesn't recognize the TestClass.
 
G

Guest

The compiler has to be able to find it. Either put it in a separate
assembly and use a reference, or put it in the APP_CODE folder.

Your statement implies that, now, I must put place all referenced code in
the APP_CODE folder or create an entirely new project and reference it. In
othere words, in VS2005, Microsoft has restricted where I can place my code.
Where I once had the ability to structure according to logical functionality,
now I can't.
 
G

Guest

Make sure that the page class and your class are in the same namespace

They are in the same namespace - the global namespace. Still, specifying a
namespace doesn't work either.

Roy
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Roy,

This is true. Since there is no more project files for websites you need to
follow predefined folder structure. you can add additional code floders
using the <codeSubDirectories> in the web.config, but all code folders have
to be subfolders of App_Code.
 
G

Guest

I appreciate your reply and I believe you, although, I don't like it.
However, since this is the case, Team Studio should automatically place code
in the APP_CODE folder and not prompt you for permission. It's misleading.
 

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

Top