|
-
Dec 3rd, 2006, 08:07 PM
#1
Thread Starter
Addicted Member
creating a namespace in a single source file
i prefer single-source pages to using code-behind pages when i develop using C#, but when i attempt to create a namespace in the .aspx page within the <script runat="server"></script> tags, i get the following error:
Code:
Invalid token 'namespace' in class, struct, or interface member declaration
How can I get around this.
my second question is in addition to creating namespaces in the same single-source page, how can i create namespaces that are project-wide in scope, the same way classes are via a Class (.cs) file?
-
Dec 3rd, 2006, 08:34 PM
#2
Re: creating a namespace in a single source file
You seem to be a little vague on exactly what a namespace is. Namespaces are not entities in themselves. They are conceptual groupings of types.
To answer your first question, you cannot specify a namespace within a type. All namespace specifications must be made outside any type declarations. Basically a namespace specification must be outside everything within a code file, other than 'using' statements.
As for the second question, namespaces are already project-wide. Like I said, namespaces are not entities in themselves. Your project has a default namespace that is the same as the project name by default. That means that every type you declare is under that namespace. You can then specify other namespaces in specific files so that the types in that file are under both the default and specified namespace. Now, you can specify the same namespace in more than one file, so types in each file will be in the same namespace. You can also specify the same namespace in more than one project.
For instance, the root namespace for all my projects is "Wunnell". I have created an class library named Wunnell.Windows.Forms.dll in which the default namespace is Wunnell.Windows.Forms. It contains al my custom controls. If I was to create an application and I wanted a custom control specifically for that application I could make the default namespace Wunnell and specify the namespace Windows.Forms in a particular file. Any type I declare in that file will then be under the Wunnell.Windows.Forms namespace, just like the types in the class library which is a completely separate assembly.
-
Dec 3rd, 2006, 08:50 PM
#3
Thread Starter
Addicted Member
Re: creating a namespace in a single source file
jmcilhinney, i appreciate your reply, but i'm afraid i'm still a little confused.
i've always thought that namespaces are basically a means of encapsulating similar classes.
i understand what you are saying that you cannot specify a namespace within a type, and as i understand it, with c#, all code is contained within a class which would explain perhaps why i cannot easily create a new namespace inside the script tags in a single-source file.
well, i created a generic shape namespace in a code-behind page, outside of the _Default class definition, which contains rect and triangle classes and instantiated those classes within Page_Load and it worked fine:
Code:
namespace shape {
public class rect {
}
public class triange {
}
}
i just wanted to do the same thing somehow in a single-source file.
well, perhaps you already answered my question and i just need to reread your post a few more times until it sinks in.
-
Dec 3rd, 2006, 09:40 PM
#4
Re: creating a namespace in a single source file
I don't do ASP.NET so I don't know exactly how ASP.NET code files work with the script code and such, but the principles of namespaces are the same. Namespaces do not encapsulate types, they group them. Let's say that you have a large group of people and you want to separate them into groups. You might put all the people with red shirts into one group, all those with blue shorts into another, etc. Now you have a bunch of people with red shirts all standing together, but there is no tangible entity called PeopleWithRedShirts. You might then tell all the people to mingle again, but no matter where each person is they are still a member of the PeopleWithRedShirts group.
Namespaces are basically the same sort of thing. They are a conceptual grouping of types in which each tyoe name is unique. It is usually the case that the types within a namespace have something in common, although in the case of an application it may just be that they're used in that application. They may not have any other noticeable similarity. The namespace doesn't say anything about what data the object contains, how it behaves or even where it is declared. As I said, you might have two types declared in two different assemblies that are members of the same namespace. Those two assemblies may even have been created by two different developers who had no knowledge of each other. Namespaces are not specific entities, they are just a grouping that has been given a name. Same name means same namespace, whether it be intentional or not.
Last edited by jmcilhinney; Dec 4th, 2006 at 05:49 PM.
Reason: Fixed some embarassing spelling
-
Dec 4th, 2006, 01:58 PM
#5
Fanatic Member
Re: creating a namespace in a single source file
I could be wrong, but I'm pretty sure the reason ASP.NET code files don't like namespaces within the scripting is because the engine takes the scripts and dynamically builds a class for the page the first time the page is accessed. When a page is accessed ASP.NET runs code in the dynamically generated class to handle the request. So, if you put a namespace declaration in your script I'm pretty sure the code will end up inside a class definition, and that's why it fails.
The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|