How to raise an events when global variable changed in vb.net

1.5k views Asked by At

I've global variables in module level with below COM-interface types.

Imports System.IO
Imports simpackcomslvLib
Imports simpackcompostLib

Module Globals
    Public Srv As SpckCOMApp
    Public Mdl As IScrModel
    Public Post As PostComApp
    Public Res As PostComProject
End Module

In another classes some of my procedures change their object values. I'd like to run some of my procedures which made some changes on my tool's GUI when the for example Mdl value is changed.

I tried with below method which made for an integer type parameter but i didnt succeded for my case, i think because of their object(I types belongs to COM-interface.

Public Class myVar
    Private mValue As Integer
    Public Event VariableChanged(ByVal mvalue As Integer)
    Public Property Variable() As Integer
        Get
            Variable = mValue
        End Get
        Set(ByVal value As Integer)
            mValue = value
            RaiseEvent VariableChanged(mValue)
        End Set
    End Property
End Class

Usage of above code in an example

Public Class Form1
    Private WithEvents test As New myVar
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        test.Variable = CInt(TextBox1.Text)
    End Sub
    Private Sub VariableChanged(ByVal NewValue As Integer) Handles test.VariableChanged
        MessageBox.Show(NewValue)
    End Sub
End Class

Is there anyway to implement my below variables in a module such a way also using in module level is wrong should i move them under the class?

Module Globals
    Public Srv As SpckCOMApp
    Public Mdl As IScrModel
    Public Post As PostComApp
    Public Res As PostComProject
End Module
0

There are 0 answers