VB.NET 2010, Framework 3.5
Question / problem with global scope in a Windows Service
Barebones Window Service with two default Classes, Class1 and Class2
Class1 looks like this.
Public Class Class1
Public Hi As String = "Hi"
End Class
The main Service Class 'OnStart' below. obj1 looks like it should have global scope
Public Class Service
Public obj1 As New Class1 ' need obj1 to have global scope
Protected Overrides Sub OnStart(ByVal args() As String)
End Sub
End Class
However, trying to access the global obj1 within Class2 generates the error "obj1 is not declared. It may be inaccessible due to its protection level"
Public Class Class2
Public Sub SayHi()
MsgBox(obj1.Hi) ' error here, obj1 is out of scope
End Sub
End Class
In a non-Service app, where Sub Main replaces Sub OnStart, obj1 is visible everywhere. All the other classes can see obj1 until flow goes out of Sub Main.
Does anyone know how to get around this?