Moving Code Breaks Namespace

W

william.oram

I'll do my best to bring a rather complicated, multifile problem into
a single newsgroup question. It's a challenge to balance accuracy with
simple code that demonstrates the problem.

Assume an external .cs file containing a class MyClass:

namespace vortex
{
public class MyClass
{
public int number;

public MyClass(SqlDataReader r)
{
number = Convert.ToInt32(r["number"]);
}
}
}

Now consider an .aspx file with a chunk of C# in the head.
GetDetails() is a code-behind function that pulls data from SQL,
creates a new MyClass using a passed SqlDataReader, and returns the
new instance to 'details':

<HTML>
<HEAD>
<title>[ UT TeleCampus -- vorTex 2 ]</title>
<%
vortex.MyClass details = GetDetails();
details.number++; // (*)
%>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/
intellisense/ie5">
<LINK title="magicstyle" href="css/vortex.css" type="text/css"
rel="stylesheet">
<script type="text/javascript" src="includes/jsFunctions.js"></
script>
</HEAD>
(etc)

This works. But move the (*) line down like this, and I get the error
'The type or namespace name 'details' could not be found (are you
missing a using directive or an assembly reference?)'.


<HTML>
<HEAD>
<title>[ UT TeleCampus -- vorTex 2 ]</title>
<%
vortex.MyClass details = GetDetails();
%>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/
intellisense/ie5">
<LINK title="magicstyle" href="css/vortex.css" type="text/css"
rel="stylesheet">
<script type="text/javascript" src="includes/jsFunctions.js"></
script>
</HEAD>
(some HTML)
<div>
<%
details.number++; // (*)
%>
</div>

Another .aspx file in the same project does pretty much the same thing
with no complaints. I realise that it's treating 'details' as if it
were a namespace, but changing it to 'vortex.details.number++' doesn't
impact it any.

1) What can I do to fix this?
2) What could cause Visual Studio to treat this code properly in one
file and improperly in another?

Thanks!
 
C

Chris Shepherd

Now consider an .aspx file with a chunk of C# in the head.
GetDetails() is a code-behind function that pulls data from SQL,
creates a new MyClass using a passed SqlDataReader, and returns the
new instance to 'details': [...]
This works. But move the (*) line down like this, and I get the error
'The type or namespace name 'details' could not be found (are you
missing a using directive or an assembly reference?)'. [...]
Another .aspx file in the same project does pretty much the same thing
with no complaints. I realise that it's treating 'details' as if it
were a namespace, but changing it to 'vortex.details.number++' doesn't
impact it any.

1) What can I do to fix this?
2) What could cause Visual Studio to treat this code properly in one
file and improperly in another?

Umm, it doesn't look to me like it's treating 'details' as a namespace,
it looks like it just doesn't know what details is, so it's looking for
a Type or a namespace that match. That means your variable isn't scoped
properly.

I'm very rusty on my ASP.NET, but have you tried moving the
"vortex.MyClass details" declaration to above the initial HTML output?


Chris.
 
W

william.oram

Now consider an .aspx file with a chunk of C# in the head.
GetDetails() is a code-behind function that pulls data from SQL,
creates a new MyClass using a passed SqlDataReader, and returns the
new instance to 'details': [...]
This works. But move the (*) line down like this, and I get the error
'The type or namespace name 'details' could not be found (are you
missing a using directive or an assembly reference?)'. [...]
Another .aspx file in the same project does pretty much the same thing
with no complaints. I realise that it's treating 'details' as if it
were a namespace, but changing it to 'vortex.details.number++' doesn't
impact it any.
1) What can I do to fix this?
2) What could cause Visual Studio to treat this code properly in one
file and improperly in another?

Umm, it doesn't look to me like it's treating 'details' as a namespace,
it looks like it just doesn't know what details is, so it's looking for
a Type or a namespace that match. That means your variable isn't scoped
properly.

I'm very rusty on my ASP.NET, but have you tried moving the
"vortex.MyClass details" declaration to above the initial HTML output?

Chris.

As I waited for an answer to this thread, I kept working on the page,
avoiding anything that involved MyClass. When I tried the page again
as shown in Example 2, it worked! I only added a bunch of form
elements...nothing that would confuse Visual Studio's C# compiler.
Bizarre.

I think I'll do as you recommended anyway, as it makes more sense
there.
 
W

william.oram

Now consider an .aspx file with a chunk of C# in the head.
GetDetails() is a code-behind function that pulls data from SQL,
creates a new MyClass using a passed SqlDataReader, and returns the
new instance to 'details': [...]
This works. But move the (*) line down like this, and I get the error
'The type or namespace name 'details' could not be found (are you
missing a using directive or an assembly reference?)'. [...]
Another .aspx file in the same project does pretty much the same thing
with no complaints. I realise that it's treating 'details' as if it
were a namespace, but changing it to 'vortex.details.number++' doesn't
impact it any.
1) What can I do to fix this?
2) What could cause Visual Studio to treat this code properly in one
file and improperly in another?

Umm, it doesn't look to me like it's treating 'details' as a namespace,
it looks like it just doesn't know what details is, so it's looking for
a Type or a namespace that match. That means your variable isn't scoped
properly.

I'm very rusty on my ASP.NET, but have you tried moving the
"vortex.MyClass details" declaration to above the initial HTML output?

Chris.

Er, so the problem is back...or rather, the problem never left. Here's
why: for some VERY odd reasons beyond my understanding, inserting C#
code between <form></form> confuses the compiler. So this works:

<body>
<form>
[stuff]
</form>
<% Response.Write(details.number); %>
</body>

But this doesn't:

<body>
<form>
<% Response.Write(details.number); %>
</form>
</body>

This was the first time in the project I needed <form> tags, hence the
first time I've encountered this. Any thoughts?
 
A

Ashot Geodakov

What I don't understand is why you are taking such a painful way of writing
..aspx as inserting code blocks between HTML tags...

Why can't you go the standard path, separate HTML and C# code (use
"codebehind"), and just write event handlers?

You can achieve pretty much anything in Page_Load() handler. Declare UI
elements in HTML with runat="server" attribute, and manipulate these
elements in C# any way you want...

Now consider an .aspx file with a chunk of C# in the head.
GetDetails() is a code-behind function that pulls data from SQL,
creates a new MyClass using a passed SqlDataReader, and returns the
new instance to 'details': [...]
This works. But move the (*) line down like this, and I get the error
'The type or namespace name 'details' could not be found (are you
missing a using directive or an assembly reference?)'. [...]
Another .aspx file in the same project does pretty much the same thing
with no complaints. I realise that it's treating 'details' as if it
were a namespace, but changing it to 'vortex.details.number++' doesn't
impact it any.
1) What can I do to fix this?
2) What could cause Visual Studio to treat this code properly in one
file and improperly in another?

Umm, it doesn't look to me like it's treating 'details' as a namespace,
it looks like it just doesn't know what details is, so it's looking for
a Type or a namespace that match. That means your variable isn't scoped
properly.

I'm very rusty on my ASP.NET, but have you tried moving the
"vortex.MyClass details" declaration to above the initial HTML output?

Chris.

Er, so the problem is back...or rather, the problem never left. Here's
why: for some VERY odd reasons beyond my understanding, inserting C#
code between <form></form> confuses the compiler. So this works:

<body>
<form>
[stuff]
</form>
<% Response.Write(details.number); %>
</body>

But this doesn't:

<body>
<form>
<% Response.Write(details.number); %>
</form>
</body>

This was the first time in the project I needed <form> tags, hence the
first time I've encountered this. Any thoughts?
 
W

william.oram

What I don't understand is why you are taking such a painful way of writing
.aspx as inserting code blocks between HTML tags...

Why can't you go the standard path, separate HTML and C# code (use
"codebehind"), and just write event handlers?

You can achieve pretty much anything in Page_Load() handler. Declare UI
elements in HTML with runat="server" attribute, and manipulate these
elements in C# any way you want...


(e-mail address removed) wrote:
Now consider an .aspx file with a chunk of C# in the head.
GetDetails() is a code-behind function that pulls data from SQL,
creates a new MyClass using a passed SqlDataReader, and returns the
new instance to 'details':
[...]
This works. But move the (*) line down like this, and I get the error
'The type or namespace name 'details' could not be found (are you
missing a using directive or an assembly reference?)'.
[...]
Another .aspx file in the same project does pretty much the same thing
with no complaints. I realise that it's treating 'details' as if it
were a namespace, but changing it to 'vortex.details.number++' doesn't
impact it any.
1) What can I do to fix this?
2) What could cause Visual Studio to treat this code properly in one
file and improperly in another?
Umm, it doesn't look to me like it's treating 'details' as a namespace,
it looks like it just doesn't know what details is, so it's looking for
a Type or a namespace that match. That means your variable isn't scoped
properly.
I'm very rusty on my ASP.NET, but have you tried moving the
"vortex.MyClass details" declaration to above the initial HTML output?
Chris.
Er, so the problem is back...or rather, the problem never left. Here's
why: for some VERY odd reasons beyond my understanding, inserting C#
code between <form></form> confuses the compiler. So this works:
<body>
<form>
[stuff]
</form>
<% Response.Write(details.number); %>
</body>
But this doesn't:
<body>
<form>
<% Response.Write(details.number); %>
</form>
</body>
This was the first time in the project I needed <form> tags, hence the
first time I've encountered this. Any thoughts?

When I deal with GUI elements, that's what I use. When I want to
output HTML that can be mass-produced across several pages, .ascx
controls work too. But when you want to print a simple string that has
its origins in a SQL Server database to a web page, I'm not aware of
any other way than an inline <% Response.Write(variable) %>. It's
clear what to expect, and I don't see how it could be made simpler.
 
A

Ashot Geodakov

When I deal with GUI elements, that's what I use. When I want to
output HTML that can be mass-produced across several pages, .ascx
controls work too. But when you want to print a simple string that has
its origins in a SQL Server database to a web page, I'm not aware of
any other way than an inline <% Response.Write(variable) %>. It's
clear what to expect, and I don't see how it could be made simpler.

Nothing stops you from placing a Label on the page, give it an ID and then
refer to that label in your Page_Load method:

in your HTML:

<asp:label id="label_ID" runat="server" /> (or use the design mode and the
toolbox to drap and drop a label)

in your Page_Load:

label_ID.Text = "whatever comes from the variable";
 

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