Namespaces and classes


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Namespaces and classes

  1. #1
    o11011 Guest

    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;
    }
    }
    }




  2. #2
    Russell Jones Guest

    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center