0

I have converted a C# source to VB containing a class Logger and a static class containing instances of Logger.

But using VB.NET, I can't figure out how to create a Module I can access from other source files in the same namespace.

I would simply like to access the object like I would do in C#:

Loggers.MyLogger.MyMethod()

or

MyNamespace.Loggers.MyLogger.MyMethod()

But that only works from the same file !

Public Shared Module Loggers
    Public Shared ReadOnly MyLogger As Logger = New Logger()
End Module

Public Class Logger
..class content...
End Class

EDIT: Since I also tried with "shared", I guess something is wrong on the project level.

Jul
  • 63
  • 5

2 Answers2

1

I don't know how you managed to miss this or fool the compiler, but on my Visual Studio, I get an error when I try to make a shared module. Modules are implicitly shared, and you're not allowed to apply the Shared keyword explicitly.

BlueMonkMN
  • 25,079
  • 9
  • 80
  • 146
1

A Module can't be declared explicitly as Shared, because it implicitly is Shared.

Change your code like following and it should work:

Public Module Loggers
    Public ReadOnly MyLogger As Logger = New Logger()
End Module

Public Class Logger
    Public Sub MyMethod()

    End Sub
End Class

References:

MatSnow
  • 7,357
  • 3
  • 19
  • 31
  • Already tried also. I have added Shared because this was not working. – Jul Jan 10 '18 at 13:48
  • Above code works. Create a new project and try it. How did you declare `MyMethod`? – MatSnow Jan 10 '18 at 13:50
  • I guess something is wrong at project level then.The method I am trying to call is Public Sub Information(ByVal message As String, ByVal Optional context As String = Nothing) _Message("Info", message, context) End Sub But might be irrelevant. Intelisense does not even find the module. – Jul Jan 10 '18 at 14:06
  • @Julien Holopainen Az Declare that `Class Logger` methods as `Shared`. Not the Class or the Module. – Jimi Jan 10 '18 at 17:03