nonoy
nonoy

Reputation: 1

Access variable in Shared Sub

is there a way to access a variable in Form_Load from an event handler?

Please dont mind the code, this is just a representation of my question.

Public Class Form

Public Sub Form_Load()
Dim x as string
x = MyClass.MethodGetValue()
End Sub

Private Shared Sub OnChanged()
MyClass2.MethodGetValue(x)
End Sub

End Class

Upvotes: 0

Views: 145

Answers (1)

OneFineDay
OneFineDay

Reputation: 9024

It's about the scope of the variable. In your situation you need a class variable. This allows it to be used anywhere inside of this class.

Public Class Form1 
  Private x As Object 'pick the datatype that matches your needs
  Public Sub Form_Load()
    x = MyClass.MethodGetValue()
  End Sub

  Private Sub OnChanged() 
    MyClass2.MethodGetValue(x)
  End Sub
End Class

Upvotes: 2

Related Questions