Same namespace structure across multiple languages?

T

Tom Kelleher

Question: Can namespaces be shared by several projects within a
solution, if those projects use different languages?

We wrote some db code in C#, and some biz logic in VB.NET. We brought
both projects within the same solution. We used identical namespace
commands for both: "Level1.Level2.Level3" Both the C# and VB.NET
classes live off "Level3."

In other VB classes, I can do this...
"Dim x as Level1.Level2.Level3.myVBClass"

In other C# classes we can do this...
"Level1.Level2.Level3.myCSClass x"

But from VB.NET we can't do this...
"Dim x as Level1.Level2.Level3.myCSClass"

If I change the namespace path slightly for C#, like making it
"LevelXXX.Level2.Level3" -- then suddenly I can see it from VB.NET.

It tells me we can't share namespaces, which seems really really weird.
Why have a programming system where (1) you can use multiple languages,
yay! - and (2) you can use namespaces to provide clean structure, yay!
-- but you can't do both at the same time?

- Tom
 
T

Tom Kelleher

I figured it out. There are two factors. One is that you need to
right-click on both projects, pick Properties, and blank out the field
marked "Root Namespace." If you don't, then that value is quietly
prepended to whatever namespace you put in the code.

The other thing is, only one project can have a reference to the other.
If the VB has a reference to the C# one, then VB can see objects from
both -- but the C# can only see its own. Or vice versa. But you can
never get both to see both, because that would require each to have a
reference to the other -- a circular reference. So the best you can do
is give one a reference to the other, and it works there.

The upshot is, I got my code to work the way I wanted.

- Tom


========================================
Tom Kelleher Consulting, Inc.
(e-mail address removed)
========================================
 
K

Kevin Yu [MSFT]

Hi Tom,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you cannot reference a C# class from a
VB.NET project under same namespace. If there is any misunderstanding,
please feel free to let me know.

Based on my research, this issue occurs becasue there is a default root
namespace set in the VB.NET project. To remove this root namespace, please
use the following steps:

1. In the Solution Explorer, right click the VB.NET project and select
properties from the popup menu.
2. Select Common Properties/General from the left pane.
3. Clear the text in the Root namespace textbox.
4. Click OK.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
K

Kevin Yu [MSFT]

Hi Tom,

Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

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