vendredi 30 novembre 2018

How do you pass an object type to a class?

This is a distilling of my actual case scenario which involves LiteDB. It simplifies the problem to a broader .net question.

Let's say I have 3 classes:

Public Class Control
    Public Property ID As Integer
    Public Property ControlName As String
End Class

Public Class Controller
    Public Property ID As Integer
    Public Property ControllerName As String
    Public Controls As List(Of Control)
End Class


Public Class Console
    Public Property ID As Integer
    Public Property ConsoleName As String
    Public Controllers As List(Of Controller)
End Class

I map Controller and Console to two separate BindingSources via the Data Source designer.

I have a Custom Control which contains a ComboBox. Instances of this are bound to either a List(Of Control) or List(Of Controller) for pages editing both Controller and Console lists.

Now, I want the custom control to update the relevant property in both Controller and Console, with my passing it the property whoes ID feild I wish updating with the SelectedValue of the combobox. Hopefull, the pseudo code below illustrates this:

'Custom Control with ComboBox:
Public Class UserControl

    Public bs As BindingSource ' Can contain rows of Controller or Console
    Public CollectionName As String

    Dim WithEvents KComboBox As ComboBox

    Private Sub ComboChanged() Handles KComboBox.SelectionChangeCommitted

        ' [PseudoCode]
        DirectCast(bs.Current, Controller).[CollectionName].ID = KComboBox.SelectedValue
        ' Resolves to:
        DirectCast(bs.Current, Controller).[Controls.ID] = KComboBox.SelectedValue
        ' or:
        DirectCast(bs.Current, Console).[Controllers.ID] = KComboBox.SelectedValue

    End Sub

End Class

And I would set up the Custom comboxes thus:

    Dim cbControls As New UserControl With {.bs = ConrollerBS, .CollectionName = "Controls"}
    Dim cbControllers As New UserControl With {.bs = ConsoleBS, .CollectionName = "Controllers"}

I think this may involve Reflection? I tried to read up on that, but it made the top of my head blow off.

How can I achieve the above?





Aucun commentaire:

Enregistrer un commentaire