-
Namespaces and classes
Is there any reason why I can't name my namespace and class the same name
and then use it in another namespace? I tried the following and I get
internal C# compiler errors. (forgive my syntax i am doing this from memory)
I believe it's not possible because the compiler gets confused between the
namespace and class.
using System;
using B;
namespace A{
void main(void){
B myb = new B();
console.write(myb.myfunc("test"));
}
}
namespace B{
class B{
void myfunc(string s){
return s;
}
}
}
-
Re: Namespaces and classes
You can have the same name for namespaces and classes--you just need to
differentiate between the function and the namespace. But there are several
other things wrong with your code.
* You need a class to contain the Main method. Right now, it's directly
inside the A namespace.
* The method name is "Main" not "main" and it takes one of these two
signatures:
static int Main(string[] args) or static void Main(string[] args)
* There's no such object as "console", it's Console, and the method is
"Write", not "write".
* Both the class B and the myfunc method must be marked Friend or Public to
be accessible.
* your myfunc method is marked "void", but returns a String.
* the "using B;" line at the top won't help in this case, because you have
to use the full namespace.class names anyway.
Here's the fixed code. If you try this, note that you must set the Startup
Object to "A.A" (namespace A, class A).
Create a new cs file, delete all the default code in the file, then paste in
this code:
using System;
namespace A{
public class A {
static void Main(string[] args) {
B.B myb = new B.B();
Console.Write(myb.myfunc("test"));
}
}
}
namespace B{
public class B{
public string myfunc(string s){
return s;
}
}
}
HTH,
Russell Jones
Sr. Web Development Editor,
DevX.com
"o11011" <o11011@hotmail.com> wrote in message
news:3c485f1e@147.208.176.211...
> Is there any reason why I can't name my namespace and class the same name
> and then use it in another namespace? I tried the following and I get
> internal C# compiler errors. (forgive my syntax i am doing this from
memory)
> I believe it's not possible because the compiler gets confused between the
> namespace and class.
>
> using System;
> using B;
> namespace A{
> void main(void){
> B myb = new B();
> console.write(myb.myfunc("test"));
> }
> }
>
> namespace B{
> class B{
> void myfunc(string s){
> return s;
> }
> }
> }
>
>
>
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
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks