0

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?

Willy
  • 9,848
  • 22
  • 141
  • 284
  • Remove the `Imports`? – GSerg Nov 19 '19 at 11:58
  • @GSerg If I remove the import and I perform Constants.Item1 it shows me an error saying 'Item1' is not a member of 'Microsoft.VisualBasic.Constants'. So how can I tell the compiler Item1 is a member of my assembly MyCompany.Constants? – Willy Nov 19 '19 at 12:02
  • Remove the `Imports` and use `MyCompany.Constants.Item1`, or change the Imports to `Imports MyCompany` and use `Constants.Item1`? – GSerg Nov 19 '19 at 12:03
  • @GSerg If I remove the Imports and the use MyCompany.Constants.Item1 then compiler says 'Constants' is not a member of 'MyCompany'. Also, I use Imports MyCompany and then use Constants.Item1 compiler says 'Item1' is not a member of 'Microsoft.VisualBasic.Constants'. – Willy Nov 19 '19 at 12:10
  • [Works for me](https://i.stack.imgur.com/UhfhR.png). Are you sure you don't have other Imports, including one for `Microsoft.VisualBasic` (which you should not have because it's implicit)? – GSerg Nov 19 '19 at 12:19
  • @GSerg Yes, that's the problem, I am referencing as well Microsoft.VisualBasic (but I am not doing Imports Microsoft.VisualBasic) because I need it for other things – Willy Nov 19 '19 at 12:31
  • 1
    It's fine if Microsoft.VisualBasic is referenced. It is imported by default (Project properties - References - Imported namespaces). All VB projects have it imported by default and it does not brake it. – GSerg Nov 19 '19 at 12:33
  • @GSerg I understand you but I do not understand why if I add a reference to MyCompany.Constants below problems appear: 1) Imports MyCompany.Constants does not work, Constants is not recognized. 2) Without using imports and using Constants.Item1 compilers says Microsoft.VisualBasic does not contain a member 'Item1'. – Willy Nov 19 '19 at 12:46
  • 1
    Create two new projects, one Class library and one Console. Put the constants in the Class library and compile. Reference the dll from the Console. Add the `Imports MyCompany` to the console. Does it work? – GSerg Nov 19 '19 at 12:53
  • @GSerg Yes, I have done what you say and it is working so checking the project where reference is added I have detected that targets Net 3.5 and the assembly referenced MyCompany.Constants was in Net 4.5. Changing MyCompany.Constants into Net 3.5 works like a charm. Thx! – Willy Nov 19 '19 at 15:32

2 Answers2

0

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.

dbasnett
  • 11,334
  • 2
  • 25
  • 33
-1

Try this :

Imports MyCompany

Module Module1

    Sub Main()

        Dim test As String = MyCompany.Constants.Item1

        Console.WriteLine(test)
        Console.ReadKey()

    End Sub

End Module
TontonVelu
  • 471
  • 2
  • 8
  • 21