Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I make a class property behave like the Checked property on a RadioButton?
    primarykey
    data
    text
    <p>I have a very simple class that holds a few public properties – ID, Text, Colour, etc. and a Boolean called 'SelectedItem'. Just like the <code>Checked</code> property on RadioButtons, only one item can have its <code>SelectedItem</code> property set to <code>True</code> within a particular group. Note: there will be several separate groups (lists), each allowed to have only one item with <code>SelectedItem = True</code>.</p> <p>My first thought was that I would probably just have to handle everything outside of the class, setting <code>SelectedItem</code> to <code>False</code> for every other item in a particular list when another item was selected. But that seems rather inelegant to me. So I've been trying to think of how this might be done <em>within</em> the class. For example: could I perhaps have a private string property called say "GroupName" – set in the <code>New</code> sub when adding a new instance of the class – and then use a private shared method to set every item's <code>SelectedItem</code> property to <code>False</code>, <em>provided</em> the item has the same <code>GroupName</code> as the newly selected item? I would have a go at doing this but I have no idea how to enumerate every instance of a class from within that class, or whether that's even possible. Is it? Or is there another (better) way to achieve my goal? </p> <hr> <p><strong>EDIT</strong></p> <p>Thanks for all the suggestions and comments, they're all very much appreciated. </p> <p>On the back of comments by Tim and Cyborgx37 I've decided to follow their advise and use a CollectionBase as an item manager class. Here is a cut-down version of what I've got so far:</p> <pre><code>Public Class ResourceItem Public ID As Integer Public Text As String Public SelectedItem As Boolean End Class Public Class ResourceItemsManager Inherits System.Collections.CollectionBase Public Sub Add(ByVal iID As Integer, ByVal sText As String) Dim newResItem As New ResourceItem With newResItem .ID = iID .Text = sText End With List.Add(newResItem) End Sub Default Public ReadOnly Property Item(ByVal Index As Integer) As ResourceItem Get If Index &lt; Count And Index &gt;= 0 Then Return CType(List.Item(Index), ResourceItem) Else Return Nothing End If End Get End Property Public Sub ClearAll() List.Clear() End Sub Public Sub SelectItem(ByVal Index As Integer) If Index &lt; Count And Index &gt;= 0 Then For i As Integer = 0 To List.Count - 1 List.Item(i).SelectedItem = i = Index Next Else 'Exception code here End If End Sub End Class </code></pre> <p>As can be seen: instead of instantiating a new <code>ResourceItem</code> and passing that as an argument to the manager's <code>Add</code> procedure, I'm simply passing the details of the new item and the procedure is creating the item from those. I don't know whether this is a good or bad idea – please advise – but I've done it because I couldn't figure out how to make the SelectedItem property only writeable by the manager, so I wanted to avoid having directly accessible objects that could have their <code>SelectedItem</code> property set to <code>True</code> without it deselecting all the other items in the same group. Of course that still leaves the possibility of setting a variable to one of the manager's list items and setting it from there, so I would still like to know how I could prevent that, if possible.</p> <hr> <p><strong>UPDATE</strong><br> I didn't use the code above in the end, deciding instead to go with Cyborgx37's solution – which seems to work perfectly well for me despite the warnings about best practice. I also realised I would need to use the same class in another scenario where multiple items <em>could</em> be selected, so it was easy to add a Boolean property to the manager to enable that.</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.
 

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