How to: Respond to the Solver Data Binding Event

You can subscribe to the data binding event that is raised by a solver and respond to the event. The following procedure demonstrates how to add a data binding event and change the VariableUpperBound field in response to the event.

To respond to the solver data binding event

  1. Add the following Imports or using statements to the top of the Program code file.

    Imports Microsoft.SolverFoundation.Common
    Imports Microsoft.SolverFoundation.Services
    
    using Microsoft.SolverFoundation.Common;
    using Microsoft.SolverFoundation.Services;
    
  2. Declare a SolverContext object to create a model.

    Private WithEvents context As SolverContext
    
    private SolverContext context;
    
  3. Add the following line of code to your model to subscribe to the solver data binding event.

    context = SolverContext.GetContext()
    Dim model As Model = context.CreateModel()
    
    context = SolverContext.GetContext();
    Model model = context.CreateModel();
    context.DataBinding += new EventHandler<DataBindingEventArgs>(context_DataBinding);
    
  4. Implement the context_DataBinding method. The following code changes the VariableUpperBound field. First, the code enumerates the indexes for every decision in the model by using the Indexes property, which is used as an array because decisions can have multiple indexes. The ComputeUpperBoundForIndex method is business logic that is user-defined.

    Private Sub context_DataBinding(ByVal sender As Object, ByVal e As DataBindingEventArgs) Handles context.DataBinding
        For Each indexes As Object() In e.Indexes
            e(SolverProperties.VariableUpperBound, indexes) = ComputeUpperBoundForIndex(indexes)
        Next
    End Sub
    
    static void context_DataBinding(object sender, DataBindingEventArgs e) {
      foreach (object[] indexes in e.Indexes) {
        e[SolverProperties.VariableUpperBound, indexes] =
            ComputeUpperBoundForIndex(indexes);
      }
    }
    

See Also

Reference

DataBindingEventArgs

Concepts

Developing with Solver Foundation Services (SFS)