Subscribe to Class Changes?

Suamere

Member
Joined
Aug 23, 2012
Messages
11
Programming Experience
5-10
I want to subscribe to changes in clsProperties because the changes are performed from Main and I want to take action in clsClass

Public Class MainClass
  Public Sub Main
    Dim MyInstance As New clsClass

    MyInstance("Me").Value = myName
    MyInstance("Me").Mask = myObject

    MyInstance("You").Value = yourName
    MyInstance("You").Mask = yourObject

  End Sub
End Class


Public Class clsClass
  Private oPC2 As New Dictionary(Of String, clsProperties)

  Default Property P(ByVal strString As String) As clsProperties
    Get
      If oPC2.ContainsKey(strString) = False Then _
        oPC2.Add(strString, New clsProperties)
      Return oPC2(strString)
    End Get
    Set(ByVal value As clsProperties)
    End Set
  End Property

End Class


Public Class clsProperties
  Public Property Value As String
  Public Property Mask As Object
End Class


This is a quick Breakdown:
I want Zero overhead in Main(), this is where user developers work.

MyInstance("x") automatically calls the Get clause of the Default Property of clsClass, creates or adds the ("x") to a dictionary(of string, clsProperties)... and returns that. After that, the Property of clsProperties is called (either Value or mask) and assigned whatever is assigned.

When one of these properties are set or changed, I want to loop through every dictionary item in clsClass and validate each one's clsProperties values.
 
Last edited:
Note that if I call an action within the "Get" of clsClass's default property, it happens prior to the clsProperties Property being set, So that is too early, I need it to happen AFTER the .Value evaluates with 0 overhead in the exposed Main class.

Developer (in Main) does this:
MyInstance("You").Value = yourName
Method in clsClass (After yourName is set to the .Value) loops through the dictionary and validates "Me" and "You"

I don't need help making the Method, just subscribing to when it is called.

If there's a way I'm not thinking about to use the "Set" property in clsProperties' Properties... To call the APPROPRIATE instance (MyInstance) of clsClass's method... that would be perfect.
 
Something like this Theoretically? I don't know how to find out how to reference the "Current Instance" being called from from within the calling class's method...

Public Class MainClass
  Public Property MyInstance As New clsClass
  Public Sub Main
    MyInstance("Name").Value = "Sua"
  End Sub
End Class


Public Class clsClass
  Private oPC2 As New Dictionary(Of String, clsProperties)
    '...
  Function ValidateDictionary()
    'Method to validate this instance's dictionary properties
  End Function

End Class


Public Class clsProperties
  Public Property Value As String
    Get
    End Get
    Set(ByVal value As String)
    'MainClass.MyInstance.ValidateDictionary()
    End Set
  End Property
  '...
End Class
 
Last... more generic way of looking at it.

If I do:
instance.clsOne.clsTwo.Property = "lol"
How do I follow up with:
instance.clsOne.checkProperties

Without having to write that right there because I want 0 overhead in the main method.
 
So you're saying that you want to receive notification when something happens, right? That's exactly what events do. What you're talking about might require more than one level, e.g. when object 1 raises an event it is handled by object 2 and then object 2 in turn raises an event of its own. If you need more information on events then you might follow the Blog link in my signature and check out my post on Custom Events.

By the way, you really should be inheriting System.Collections.ObjectModel.KeyedCollection, not Dictionary.
 
Sorta. When
MainClass.MyInstance.clsClass.Dictionary("DictKey").clsProperties.Property
changes, I want
MainClass.MyInstance.clsClass.Function
to execute

The easiest way would be like this:

  Sub MainClass
      MyInstance("Name").Value = myName
      MyInstance.ValidateDictionary
  End Sub

But I want 0 overhead in that front-end working class. So my thought was maybe to place an eventhandler in the PropertyClass so when .Value was set, clsClass.myFunction is called with the instance in which the .Value property was set.
'in clsClass, my Dictionary has multiple instances of clsProperties:
Public Class clsClass
  Private oPC2 As New Dictionary(Of String, clsProperties)
    '...
  Function ValidateDictionary()
    'Method to validate this instance's dictionary properties
  End Function
End Class

'in clsProperties Value Set area:
    Set (ByVal value as String)
      'What object instance am I operating as?...
      'Well, call THAT instance of clsClass.ValidateDictionary
    End Set
 
Here's a clean way of putting it... The following code works as it should. But the current goal is to make HandlingClass.ValueSet() fire (for the appropriate InstantiatingClass instance), whenever HandlingProperties.Value1 or .Value2 are set from InstantiatingClass; With little changes to InstantiatingClass (Small Changes may be okay). Here is the working code:

Public Class InstantiatingClass
    Public MyInstance1 As New HandlingClass
    Public MyInstance2 As New HandlingClass
    'Make little or no changes to this Class
    Public Sub Main()
        MyInstance1("One").Value1 = "abcd"
        MyInstance1("Name").Value2 = "efg"
        MyInstance1("Power").Value1 = "hijk"
        MyInstance2("Boolean").Value2 = "lmnop"
        MyInstance2("Cards").Value1 = "qrstuv"
        MyInstance2("Tree").Value2 = "wxyz"
    End Sub
End Class

Public Class HandlingClass
    Private oDic As New Dictionary(Of String, HandlingProperties)
    Default Property defProp(ByVal strString As String) As HandlingProperties
        Get
            If oDic.ContainsKey(strString) = False Then _
                oDic.Add(strString, New HandlingProperties)
            Return oDic(strString)
        End Get
        Set(ByVal value As HandlingProperties)
        End Set
    End Property
    Public Sub ValueSet()
        'Your Goal
        MsgBox("You Did It!")
    End Sub
End Class

Public Class HandlingProperties
    Private Property _Value1 As String
    Private Property _Value2 As String
    Public Property Value1 As String
        Get
            Return _Value1
        End Get
        Set(ByVal value As String)
            _Value1 = value
            'Call InstantiatingClass.ThisInstance.ValueSet?
        End Set
    End Property
    Public Property Value2 As String
        Get
            Return _Value2
        End Get
        Set(ByVal value As String)
            _Value2 = value
            'Call InstantiatingClass.ThisInstance.ValueSet?
        End Set
    End Property
End Class

 
Implement INotifyPropertyChanged interface in class HandlingProperties, handle the event for all HandlingProperties instances in class HandlingClass.
 
Answer

I figured it out. And like most complicated things, it's overly simple. As I mentioned before, the GET portion of the HandlingClass is called prior to the value being set, so handling the event there is too early. However, you can see what I did below:

Public Class InstantiatingClass
Public MyInstance1 As New HandlingClass
Public MyInstance2 As New HandlingClass
'Make little or no changes to this Class
Public Sub Main()
MyInstance1("One").Value1 = "abcd"
MyInstance1("Name").Value2 = "efg"
MyInstance1("Power").Value1 = "hijk"
MyInstance2("Boolean").Value2 = "lmnop"
MyInstance2("Cards").Value1 = "qrstuv"
MyInstance2("Tree").Value2 = "wxyz"
    End Sub
End Class

Public Class HandlingClass
    Private oDic As New Dictionary(Of String, HandlingProperties)
    Default Property defProp(ByVal strString As String) As HandlingProperties
        Get
            If oDic.ContainsKey(strString) = False Then _
                oDic.Add(strString, New HandlingProperties)
            oDic(strString).myInstance = Me
            Return oDic(strString)
        End Get
        Set(ByVal value As HandlingProperties)
        End Set
    End Property
    Public Sub ValueSet()
        'Your Goal
        MsgBox("You Did It!")
    End Sub
End Class

Public Class HandlingProperties
    Public Property myInstance As HandlingClass
    Private Property _Value1 As String
    Private Property _Value2 As String
    Public Property Value1 As String
        Get
            Return _Value1
        End Get
        Set(ByVal value As String)
            _Value1 = value
            myInstance.ValueSet()
        End Set
    End Property
    Public Property Value2 As String
        Get
            Return _Value2
        End Get
        Set(ByVal value As String)
            _Value2 = value
            'Call InstantiatingClass.ThisInstance.ValueSet?
        End Set
    End Property
End Class
 
Back
Top