views:

97

answers:

3

I have two projects : Menu and Module and they are both in the same namespace foobar.

I am currently referencing the module project from the Menu project to open up certain controls on a tab control in my menu. However I need to launch a new control from one of my controls which is located in the Module project.

When I try referencing the menu project, it does not show up in my intellisense when I try to reference it with a using. Am I doing something wrong logically here?

Here is an example of what it is :

Project Menu

Public Void LaunchWPFControl(string pHeader,string pPath)
{
     //Code goes here to launch a WPF control in the browser
}

Project Module

//What I would love to do but doesn't work
Using Menu;
...
...
...
private void dgModule_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
     Menu.LaunchWPFControl("Accounts","AccountsControl");
}
A: 

If they're both in namespace foobar then you need

using foobar;

instead of using Menu.

It's important that you understand the terminology involved - you're talking about "a different project in the same assembly" - that's nonsensical. Visual Studio creates one assembly per project. You then talk about the projects having the same namespace in the text of your question. You need to understand the difference.

To work out the namespace of a type, open the class containing the type and look for namespace declarations:

namespace MyProject
{
    ...
}

To work out the assembly of a type, look in the project properties for the project in which it's declared - or if you're using the same solution, just add a reference from the project which wants to use the type to the project which declares the type.

Note that you specify a namespace with a using directive; you need to add a reference to an assembly in solution explorer. They're not doing the same thing. A using directive just says, "Within the code affected by this using directive, I want to be able to use types within this namespace without fully qualifying the names."

Next you've got code like this:

Menu.LaunchWPFControl("Accounts","AccountsControl");

I thought Menu was either a project name or a namespace - but now you're trying to use it as a type name. Which is it?

If this doesn't sort you out, please post full code and a more coherent description of the projects and namespaces involved. Take a step back, work out the types, namespaces and assemblies involved, and then lay it all out clearly.

Jon Skeet
+4  A: 

If you are talking about seperate projects then what you are trying to do here is a circular reference, this is not allowed. If Project Menu references Project Module, then Project Module cannot reference Project Menu.

If you need a class in Project Module to trigger something in the Menu project you need to look for an alternative way of doing it. One possible technique for achieving this is to create an event in the class in the Module project that the Menu project can subscribe to and perform the required action.

For example in Project Module:

private void dgModule_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    OnMyEvent();
}

private void OnMyEvent()
{
    EventHandler localEvent = MyEvent;
    if(localEvent != null)
    {
        localEvent(this, EventArgs.Empty);
    } 
} 

Then in Project Menu you can subscribe to this event and perform your action:

...
...
...
moduleClass.MyEvent += SomeHandler;
...
...
...
private void SomeHandler(Object sender, EventArgs e)
{
    Menu.LaunchWPFControl("Accounts","AccountsControl");
}    

As Ray Burns mentions (see comments) another way would be to define an interface of the Menu class in some shared location (either there referenced project, or some other shared project) and than you can pass implementations of that interface to the Module project.

Which way is better often depends on the abstraction you are trying to achieve with each project.

Simon P Stevens
Yes. Another technique is to define an interface in the referenced project, then implement it in the referencing project and pass your implementation object to the referenced project.
Ray Burns
@Ray: Good suggestion, I've updated the answer. Feel free to tweak it yourself.
Simon P Stevens
A: 

So Project Menu references Project Module. Then you want Project Module to reference Project Menu so a module can directly call Menu functionality?

This isn't possible, this is a circular reference. If you try to add the Menu reference to the Module project, Visual Studio won't let you.

You need to pull the stuff out of Menu that both Menu and Module want to use into a third project, and have them both reference it. Or combine Menu and Module into one project.

Matt Greer