Search code examples
vb.netproject-referenceaccess-levels

Cannot access referenced project constants


I'm having an issue using a module across multiple project in the same solution (a regular project and a test project for unit tests). I'm trying to follow the example code from http://msdn.microsoft.com/en-us/library/ms182532(v=vs.110).aspx (but using VB.NET instead of C#).

So I've got 2 projects, ZBank and ZBankTests. ZBankTests has a reference to ZBank. ZBank has a class, clsAccount (namespace BankAccount) and a module, modConstants (namespace BankConstants). clsAccount imports ZBank.BankConstants and can use its constant variables.

ZBankTests has a class, clsAccountTests (namespace ZBank.BankAccount.Tests) that imports ZBank.BankAccount and ZBank.BankConstants. I can use the BankAccount class fine, but it errors:

"'DebitAmountExceedsBalanceMessage' is not declared. It may be inaccessible due to its protection level."

I've also compiled the module into a DLL, but after adding the reference to it, neither project can import it or use its variables (tried after reading this: Using the same modules in multiple projects).

Code for the module:

Namespace BankConstants

    Module modConstants

        Public DebitAmountExceedsBalanceMessage As String = "Debit amount exceeds balance"
        Public DebitAmountLessThanZeroMessage As String = "Debit amount less than zero"

    End Module

End Namespace

Solution

  • A Module access level is Friend by default. You have to explicitly declare it as Public if you want to use it from outside the project boundaries.

    Public Module modConstants
        ' your constants
    End Module
    

    See Module statement documentation for more information.