Namespaces within Visual Studio IDE

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jan Michalski

    #1

    Namespaces within Visual Studio IDE

    Does anyone know how to incorporate classes with different namespaces within
    a project? I have two classes with the same name but different namespace.
    When I try to add the the second class of the same name the Visual Studio
    NET IDE blocks my attempt and asks whether I want to overwrite the first
    class. Which I do not want to do.

    Compiling is fine outside the IDE.






  • Rob Tillie

    #2
    Re: Namespaces within Visual Studio IDE

    Hey,
    I do not think this is possible, because you have 2 files with the same name
    in the same directory. The namespace is only of importance in the file
    itself. So rename the file, or put it in a subdirectory.

    Greetz,
    -- Rob.

    Jan Michalski wrote:[color=blue]
    > Does anyone know how to incorporate classes with different namespaces
    > within a project? I have two classes with the same name but different
    > namespace. When I try to add the the second class of the same name
    > the Visual Studio NET IDE blocks my attempt and asks whether I want
    > to overwrite the first class. Which I do not want to do.
    >
    > Compiling is fine outside the IDE.[/color]


    Comment

    • Jan Michalski

      #3
      Re: Namespaces within Visual Studio IDE

      Dear Rob,
      Thanks for responding to this.
      I have a C# file which contains the main function and two classes with the
      same name within that same file. The classes have different namespaces. This
      file and another associated with it compile outside the Visual Studio IDE. I
      could not figure out how that file could be accommodated within the Visual
      Studio NET IDE. I checked through the documentation but could not find
      anything that would get me on the right track.

      I suppose what I am interested in is how I can incorporate code from
      different namespaces into my code. Within java this is easily done in the
      various IDEs. But with Visual Studio IDE I cannot quite figure this out.

      Regards,
      Jan



      "Rob Tillie" <Rob.Tillie@stu dent.tul.edu> wrote in message
      news:Ox2#8SqXDH A.652@TK2MSFTNG P10.phx.gbl...[color=blue]
      > Hey,
      > I do not think this is possible, because you have 2 files with the same[/color]
      name[color=blue]
      > in the same directory. The namespace is only of importance in the file
      > itself. So rename the file, or put it in a subdirectory.
      >
      > Greetz,
      > -- Rob.
      >
      > Jan Michalski wrote:[color=green]
      > > Does anyone know how to incorporate classes with different namespaces
      > > within a project? I have two classes with the same name but different
      > > namespace. When I try to add the the second class of the same name
      > > the Visual Studio NET IDE blocks my attempt and asks whether I want
      > > to overwrite the first class. Which I do not want to do.
      > >
      > > Compiling is fine outside the IDE.[/color]
      >
      >[/color]


      Comment

      • Jan Michalski

        #4
        Re: Namespaces within Visual Studio IDE

        Dear Michael,

        The file structure I have is the same as yours.

        Can you briefly outline how you incorporate this file into your
        project.When I create my project it automatically creates a namespace with
        the same name as the project name. And I cannot see how to incorporate
        further namespaces. I have tried to add two classes with the same name but
        different namespaces. And then the IDE protests. Clearly this approach must
        be incorrect. How do you do it?

        Regards,
        Jan



        "Michael Mayer" <mrmayer@charte r.net> wrote in message
        news:uCeOcyrXDH A.2236@TK2MSFTN GP10.phx.gbl...[color=blue]
        > "Jan Michalski" wrote:
        > <snip>[color=green]
        > > I have a C# file which contains the main function and two classes[/color]
        > with the[color=green]
        > > same name within that same file. The classes have different[/color]
        > namespaces. This[color=green]
        > > file and another associated with it compile outside the Visual[/color]
        > Studio IDE. I[color=green]
        > > could not figure out how that file could be accommodated within the[/color]
        > Visual[color=green]
        > > Studio NET IDE. I checked through the documentation but could not[/color]
        > find[color=green]
        > > anything that would get me on the right track.[/color]
        > <snip>
        >
        > The following compiles just fine in my VS.NET 2002 Two classes both
        > named Class1. Can you post an example that doesn't work?
        > -mike
        >
        >
        > ********* all in Class1.cs **********
        >
        > using System;
        >
        > namespace Namespace1
        > {
        > class Class1
        > {
        > public Class1()
        > {
        > Console.WriteLi ne("Creating Class1 in Namespace1");
        > }
        > }
        > } // End of namespace
        >
        >
        > namespace Namespace2
        > {
        > class Class1
        > {
        > public Class1()
        > {
        > Console.WriteLi ne("Creating Clas1 in Namespace2");
        > }
        >
        > public static void Main()
        > {
        > Class1 class1_2 = new Class1();
        > Namespace1.Clas s1 class1_1 = new Namespace1.Clas s1();
        > Console.ReadLin e();
        > }
        > }
        >
        > }
        >
        >
        >[/color]


        Comment

        • Michael Mayer

          #5
          Re: Namespaces within Visual Studio IDE

          As a preface, I don't think it's very good organization to have
          mutliple classes in the same text file. I would try separating them
          out by directory, something like this:
          ProjectDirector y
          Source
          Namespace1
          Class1.cs
          Namespace2
          Class1.cs
          Bin
          Doc
          you get the idea...

          But if you do wish to put two classes into the same file, here's what
          I did:

          The way I did it originally was just to type the 2nd namespace into
          the default Class1.cs file of the project, and then compile it. Is
          this what you are doing? How does the IDE protest? When you compile?

          If you have a file called Class1.cs with two classes in it (like my
          previous post) I can add it to a new project (one that does not have a
          Class1.cs) as follows:
          In solution explorer:
          Right click on the project name:
          Add existing item
          Pick Class1.cs from the dialog
          It adds it just fine.

          Also, I can add a new class into an existing file with the wizard.
          Goto class view. You'll see the namespaces. You can add a third
          class into the file:
          Right click on the project
          Add | class
          Enter the following: Class: Class1, Namespace: Namespace3
          Hit ok
          It prompts me: Class1.cs already exists. Do you want to merge to it?
          I hit yes.

          mike


          "Jan Michalski" <jan.michalski@ blueyonder.co.u k> wrote in message
          news:zuoZa.1256 3$ES1.4305@news-binary.blueyond er.co.uk...[color=blue]
          > Dear Michael,
          >
          > The file structure I have is the same as yours.
          >
          > Can you briefly outline how you incorporate this file into your
          > project.When I create my project it automatically creates a[/color]
          namespace with[color=blue]
          > the same name as the project name. And I cannot see how to[/color]
          incorporate[color=blue]
          > further namespaces. I have tried to add two classes with the same[/color]
          name but[color=blue]
          > different namespaces. And then the IDE protests. Clearly this[/color]
          approach must[color=blue]
          > be incorrect. How do you do it?
          >
          > Regards,
          > Jan
          >[/color]


          Comment

          Working...