Share constants from a module with other projects

650 Views Asked by At

I have a VB.NET class library project which assembly name is MyCompany.Constants and it only contains a module MyModule.vb :

Public Module Constants

   Public Const Item1 As String = "Foo"
   Public Const Item2 As String = "Foo2"

End Module

The purpose of it is to share it across all the projects in other solutions.

So I add a reference to it from other projects and then I use the below expression to start working with it:

Imports MyCompany.Constants

So then I can use it by performing:

var foo = Constants.Item1

But above import expression throws an error saying:

namespace or type specified in the imports 'MyCompany.Constants' doesn't contain any public member

So How can I face this problem? How can I use public constants defined in the module in my other projects by adding a reference to it?

2

There are 2 best solutions below

0
On

Try this :

Imports MyCompany

Module Module1

    Sub Main()

        Dim test As String = MyCompany.Constants.Item1

        Console.WriteLine(test)
        Console.ReadKey()

    End Sub

End Module
0
On

Instead of using a Module try using a Class. If the intent is for this to be constants it should look like this

Public Class Constants
    Public Shared ReadOnly Item1 As String = "Foo"
    Public Shared ReadOnly Item2 As String = "Foo2"
End Class

Because the items are shared the class does not have to be instantiated.