Registering/deregistering object with list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bryan

    Registering/deregistering object with list

    A Person can belong to one group.
    I want to be able to set the group using these two methods:

    PersonA.Group = GroupA

    or

    GroupA.Add(Pers onA)

    In the Set method of Person.Group, the Person adds itself to the
    Groups list:
    Set
    _Group.Remove(M e) ' Remove self from previous group
    value.Add(Me) ' Add self to new group
    _Group = value ' Set field
    End Set

    So when using PersonA.Group = GroupA, PersonA has knowledge of what
    group it belongs to, and it also registers with GroupA.
    So immediately after doing:
    PersonA.Group = GroupA,
    ....the following is true:
    GroupA.Contains (PersonA) = True.

    Coming the other direction, when saying: GroupA.Add(Pers onA), I have
    in a OnListChanged sub of the Group class:
    (pseudo code)
    Sub ListChanged(e)
    if e.ChangeType = Add Then
    e.Person.Group= Me
    ElseIf e.ChangeType = Delete
    e.Person.Group= Nothing
    End If
    End Sub

    So after doing:
    GroupA.Add(Pers on)
    ....the following is true:
    PersonA.Group = GroupA.

    The problem is that this creates an infinite loop. When Something is
    added/removed to Group, the Person.Group setter is called. And when
    the Person.Group setter is called, it triggers the Group ListChanged
    event.

    I am working on a messy solution with shared variables that allow the
    ListChanged subs let the Person.Group setter know that it is being set
    by the ListChanged event, and vice versa so they don't call each
    other.
    I am looking for a clean pattern. How would you solve this problem?

  • Bill McCarthy

    #2
    Re: Registering/deregistering object with list

    Hi Bryan,

    try changing the set to:

    Set
    If value Is _Group Then return
    If _Group IsNot Nothing Then _Group.Remove(M e)
    value.Add(Me) ' Add self to new group
    _Group = value ' Set field
    End Set



    "Bryan" <bryanvick@gmai l.comwrote in message
    news:663ea7a6-252f-44f8-87a6-6a036a2583ca@t4 1g2000hsc.googl egroups.com...
    >A Person can belong to one group.
    I want to be able to set the group using these two methods:
    >
    PersonA.Group = GroupA
    >
    or
    >
    GroupA.Add(Pers onA)
    >
    In the Set method of Person.Group, the Person adds itself to the
    Groups list:
    Set
    _Group.Remove(M e) ' Remove self from previous group
    value.Add(Me) ' Add self to new group
    _Group = value ' Set field
    End Set
    >
    So when using PersonA.Group = GroupA, PersonA has knowledge of what
    group it belongs to, and it also registers with GroupA.
    So immediately after doing:
    PersonA.Group = GroupA,
    ...the following is true:
    GroupA.Contains (PersonA) = True.
    >
    Coming the other direction, when saying: GroupA.Add(Pers onA), I have
    in a OnListChanged sub of the Group class:
    (pseudo code)
    Sub ListChanged(e)
    if e.ChangeType = Add Then
    e.Person.Group= Me
    ElseIf e.ChangeType = Delete
    e.Person.Group= Nothing
    End If
    End Sub
    >
    So after doing:
    GroupA.Add(Pers on)
    ...the following is true:
    PersonA.Group = GroupA.
    >
    The problem is that this creates an infinite loop. When Something is
    added/removed to Group, the Person.Group setter is called. And when
    the Person.Group setter is called, it triggers the Group ListChanged
    event.
    >
    I am working on a messy solution with shared variables that allow the
    ListChanged subs let the Person.Group setter know that it is being set
    by the ListChanged event, and vice versa so they don't call each
    other.
    I am looking for a clean pattern. How would you solve this problem?
    >

    Comment

    Working...