How do create a custom event to watch when something changes

George Marsack

New member
Joined
Feb 11, 2006
Messages
2
Location
Ray, Michigan, USA
Programming Experience
1-3
Hello Everyone,

How do I create an event to watch when a Boolean variable changes values? Is this possible?

For example, I have a public function that is called to change a private shared variable, and I would like something hooked in to listen to when the private Boolean variable changes and fire off a function that would do something else when this occurred.

=====================================
Private Shared m_bLoadDefaults AsBoolean = False


Public Shared Sub LoadDefaults()
m_bLoadDefaults = True
End Sub

Private Sub
SetDefaultStates()
'Stuff goes here.
End Sub

=====================================


Ideally, I would like to call the function, like MyClass.LoadDefaults() and what occurs next causes the event to be raised which fires off another function like SetDefaultStates().

Is this possible? Can anyone help me? I would really appreciate any feedback. I'm not very good with events and such and I'm very new at VB.NET. Thanks to anyone who can help. :)


--George
 
You have misunderstood events.

If you add an event in MyClass you raise this event from inside this class when something happens. It is those who got an object instance of this class that can subscribe to this event, and subsequently do something when this event is triggered.

What you ask to do don't need events, you ask to do different stuff inside your class when a property is changed or a method is called, and this you just do without raising an event to the world outside the class.

Example:
VB.NET:
Public Class myClass
 
Private _bLoadDefaults As Boolean = False
Public Property m_bLoadDefaults() As Boolean
  Get
    Return _bLoadDefaults
  End Get
  Set(ByVal Value As Boolean)
    _bLoadDefaults = Value
    SetDefaultStates()
  End Set
End Property
 
Public Sub LoadDefaults()
  m_bLoadDefaults = True
  SetDefaultStates()
End Sub
 
Private Sub SetDefaultStates()
  'Stuff goes here.
End Sub
 
End Class
If you now either call LoadDefaults method or change m_bLoadDefaults property for an instance of myClass the SetDefaultStates method is executed too internally in that class.
 
Hmmmm.. Not quite what I had in mind. Unfortunately, I'll be calling the m_bLoadDefaults from another class all together. I've tried what you suggested, and it requires me to set LoadDefaults and SetDefaultStates sub to Shared and likewise other elements inside the SetDefaultsState sub are need to be set to shared as well. I really want to avoid this since there are a ton of elements used in this function that I would have to change and rework.

For now, I'm actually using a timer to monitor my Boolean variable. Not the most efficient, but it's working.
 
Are you sure you want everything in this class that should be accessible to the outside to be Shared ? You are not required to unless this is desired design.
There could be reasons for this, I don't understand the full purpose of this class from your code examples and what you say.
I suspect you have misunderstood classes too. And object instances versus types... :) Tell me I am wrong or elaborate.
 
Back
Top