Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is an extremely simple solution. It doesn't follow every best practice (for example, exposing the Items collection in the ItemManager is probably a bad idea), but it demonstrates the idea of a central manager and properties in the individual items which interface with the manager.</p> <pre><code>Module Module1 Sub Main() Dim l_itemManager As New ItemsManager() Dim l_item1 As New Item(l_itemManager) With {.Description = "Item 1"} Dim l_item2 As New Item(l_itemManager) With {.Description = "Item 2"} Dim l_item3 As New Item(l_itemManager) With {.Description = "Item 3"} Dim l_item4 As New Item(l_itemManager) With {.Description = "Item 4"} l_itemManager.SelectedItem = l_item1 Console.WriteLine("Item 1 = " &amp; If(l_item1.IsSelected, "Selected", "Unselected")) Console.WriteLine("Item 2 = " &amp; If(l_item2.IsSelected, "Selected", "Unselected")) Console.WriteLine("Item 3 = " &amp; If(l_item3.IsSelected, "Selected", "Unselected")) Console.WriteLine("Item 4 = " &amp; If(l_item4.IsSelected, "Selected", "Unselected")) Console.WriteLine("") l_itemManager.SelectedItem = l_item2 Console.WriteLine("Item 1 = " &amp; If(l_item1.IsSelected, "Selected", "Unselected")) Console.WriteLine("Item 2 = " &amp; If(l_item2.IsSelected, "Selected", "Unselected")) Console.WriteLine("Item 3 = " &amp; If(l_item3.IsSelected, "Selected", "Unselected")) Console.WriteLine("Item 4 = " &amp; If(l_item4.IsSelected, "Selected", "Unselected")) Console.WriteLine("") l_item3.IsSelected = True Console.WriteLine("Item 1 = " &amp; If(l_item1.IsSelected, "Selected", "Unselected")) Console.WriteLine("Item 2 = " &amp; If(l_item2.IsSelected, "Selected", "Unselected")) Console.WriteLine("Item 3 = " &amp; If(l_item3.IsSelected, "Selected", "Unselected")) Console.WriteLine("Item 4 = " &amp; If(l_item4.IsSelected, "Selected", "Unselected")) Console.WriteLine("") Console.WriteLine("Press any key...") Console.ReadKey(True) End Sub Class ItemsManager Public Event SelectedItemChanged As EventHandler Public Items As New List(Of Item) Private _item As Item Public Property SelectedItem() As Item Get Return _item End Get Set(ByVal value As Item) _item = value RaiseEvent SelectedItemChanged(sender:=Me, e:=EventArgs.Empty) End Set End Property End Class Class Item Public Event IsSelectedChanged As EventHandler Private WithEvents _manager As ItemsManager Public ReadOnly Property Manager() As ItemsManager Get Return _manager End Get End Property Private _description As String Public Property Description() As String Get Return _description End Get Set(ByVal value As String) _description = value End Set End Property Public Property IsSelected() As Boolean Get Return Me.Manager.SelectedItem Is Me End Get Set(ByVal value As Boolean) If value Then Me.Manager.SelectedItem = Me Else Me.Manager.SelectedItem = Nothing End If End Set End Property Public Sub New(ByVal manager As ItemsManager) _manager = manager Me.Manager.Items.Add(Me) End Sub Private Sub _manager_SelectedItemChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles _manager.SelectedItemChanged RaiseEvent IsSelectedChanged(sender:=Me, e:=EventArgs.Empty) End Sub End Class End Module </code></pre> <p>EDIT:</p> <p>While not perfectly inline with best practices, this code will work. Best practices are highly dependent on context, so it's hard to say what the "right" approach would be without knowing the specifics of your application. That said, the general concept of a manager and child classes (where the child classes are calling into the manager to get their state) should work in nearly all circumstances.</p> <p>I agree with @TimSchmelter, CollectionBase is going to give you the most power. But you may not need that much power for your purposes. And you'd still end up having to use code similar to what I've posted - where the children will ask the parent for their status.</p> <p>NOTE: I corrected a small bug in the code above. Setting a selected item to not selected would have resulted in nothing happening. I've changed this behavior to make the SelectedItem Nothing.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload