Namespaces within Visual Studio IDE

J

Jan Michalski

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.
 
R

Rob Tillie

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.
 
J

Jan Michalski

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
 
J

Jan Michalski

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
 
M

Michael Mayer

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:
ProjectDirectory
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
 

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