Mark Mark - 9 months ago 51 Question

Share module between projects in Visual Basic

Can one share modules in a project with other projects in a solution? If so, how does one get projects to recognize the declarations in another project?

The backstory is that I've inherited a bunch of Visual Basic code, a new language for me, which has several solutions that contain projects with repeated code. As an example, several WPF Application projects call a subroutine CheckSize() when their main window has loaded. The CheckSize() subroutine (a method of the main window) is re-implemented in each project.

As a first step and just to keep things simple, I'm trying to change CheckSize() to take a Window, and put it in a module within a new shared project. The new project compiles, but I can't get any of the other projects to recognize that CheckSize() has been declared. Despite having each project reference the new project, nothing.

I've had the same outcome using a class rather than a module. The class is public, the subroutine public and shared, and the class is imported into any file that uses CheckSize(), but no dice.

Update: Bah! When creating the shared project, I let it take the default name (ClassLibrary1) then renamed it in the solution explorer, which doesn't update the root namespace in the application section of My Project. Bah!

Answer Source

Create a class library project with a shared public declaration of CheckSize(). Add a reference to this project in all projects that use the original CheckSize(). Change all calls to the original CheckSize() to the fully qualified name of your shared class library, i.e YourSharedClassLibrary.CheckSize(window).