Note that there are some explanatory texts on larger screens.

plurals
  1. POOOP, data binding and user selections
    primarykey
    data
    text
    <p>The basic question is using data binding what are the ways you can allow the user to select the subclass to do the job?</p> <p>Specific example. I have a class that accepts a CalculationMethod (interface) to do the calculation. There are several implementations of CalculationMethod. The GUI developer wants to only use data binding to present the choices to the user.</p> <p>I have taken a few approaches. </p> <p>Easiest is to create a class that returns a list(of CalcuationMethod) for all the implementations and add a Name property to CalculationMethod for display purposes.</p> <p>Expanding on that I will sometimes create a class that uses reflection to do the same thing (finds all the classes that implement CalculationMethod). This way I don't have to remember to add new implementations but it can be bad in web applications (it is expensive) or environments that don't allow reflection.</p> <p>At times I have added an enumeration with each enum representing an implementation. There is a factory method that accepts the enumeration and returns the proper implementation. This way the GUI developer can bind to the enumeration. I often do this if the user selection must be persisted in some way.</p> <p>All the above have advantages and disadvantages. Are there other and/or better ways to do this?</p> <p>Example below. The GUI developer would bind the dropdown (or whatever) to the CalculationOptions.Calculations allowing the user to select. I won't write the other examples because you should get the idea. (I would use reflection to get all the classes that inherit CalculatoinTemplate or I would have an enum that represents all the classes that inherit.)</p> <pre><code>Public Class CalculationTemplate Public MustOverride Readonly Property Name() as string Public MustOverride Sub Calculate() End Class Public Class CalculationImp1 Public Overrides Sub Calculate() End Sub Public Overrides Readonly Property Name() as String Get Return "Imp1" End Get End Property End Class Public Class CalculationImp2 Public Overrides Sub Calculate() End Sub Public Overrides Readonly Property Name() as String Get Return "Imp2" End Get End Property End Class Public Class CalculationOptions Public Shared Function Calculations() as List(Of CalculationTemplate) Dim lst as New List(Of CalculationTemplate) lst.add(new CalculationImp1) lst.add(new CalculationImp2) Return lst End Function End Class </code></pre>
    singulars
    1. This table or related slice is empty.
    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