views:

45

answers:

1

I have a kind of weird situation ...

I have a User Control in WPF witch in turn has some other User Controls attached to it, then I have a huge C# code file with a big algorithm which needs access to the User Control UI Elements and methods, this hole process works with a Timer which sends data to the C# code file algorithm from the User Control and it needs to return and update the UI elements from the control and also to access it's methods...

Now the thing is I don't want to put this huge algorithm in the codebehind file of my control, instead I would like to access the control's UI elements and declared methods from that code file ...

What I tried so far is to actually derive the code file's class from the User Control I use, this works fine and dandy but to access the derived class I need to create a new object of it and the UI that I get shown does not get updated since it also creates a new base class object I believe ...

so I have something like:

public partial class usrctrlSimulator : UserControl
{
    public usrctrlSimulator()
    {
        this.InitializeComponent();          
    }

    public void StartSimulator()
    {
        Algorithm = new csAlgorithm();

        Algorithm.InitializeSimulator();

        timer1.Start();
    }
}

public class csAlgorithm : usrctrlSimulator
{
    public csAlgorithm()
    {
    }

    public void InitializeSimulator()
    {
         txtblkSimulatorStatus.Text = "Started"; // this element would be from the user control
    }
}

So my question is : how do I call the derived class without instantiating a new object of it, since that will cause a new user control object to be created and the displayed UI will not be updated ... or if I don't derive the Algorithm class, what possibility do I have to access the user control elements and methods ?

A: 

If you want to stick with one instance of the control and still have access to the functionality in the derived class then you need to use the derived class as the control. So instead of an instance of usrctrlSimulator, you'd use csAlgorithm everywhere.

However, I'm not sure whether this design is the best approach in your scenario. The algorithm is not really a user control so maybe deriving from usrctrlSimulator is not the ideal option. For example: UserControl has a method called ApplyTemplate(). What would be the meaning of this in csAlgorithm? You can also look at it from a different angle: Would it be reasonable to use csAlgorithm wherever you could use UserControl, e.g. when invoking UserControl.AddLogicalChild(csAlgorithm)?

A different option would be to instantiate the algorithm as a member variable in usrctrlSimulator (composite). In that case you could still use it inside the usrctrlSimulator but you would have a clear separation of two concepts: A UserControl on one hand, and the implementation of an algorithm on the other hand. In addition you could then change either one of them with only limited impact on the other.

In that case your code would look as follows:

public partial class usrctrlSimulator : UserControl
{
   public usrctrlSimulator()
   {
      this.InitializeComponent();          
   }

   public void StartSimulator()
   {
      _algorithm= new csAlgorithm();
      _algorithm.InitializeSimulator();
      timer1.Start();
   }

   private csAlgorithm _algorithm;
}

public class csAlgorithm // not a UserControl anymore
{
   public csAlgorithm()
      {
      }

   public void InitializeSimulator()
   {
      txtblkSimulatorStatus.Text = "Started"; // this element would be from the user control
   }
}
John
well this is what I tried the first time but then I don't have access to the user control elements anymore, the txtblkSimulatorStatus will not be seen from the algorithm file ... in this case I would need to know in some way what control called the csAlgorithm class and access it, but I have no ideea on how to do that
Sergiu
You could define events either in usrctrlSimulator or in csAlgorithm. For example the constructor for csAlgorithm could take txtblkSimulatorStatus as a parameter and then register for an event on that object (if it exposes events). Maybe there is a Changed or ValueChanged event on txtblkSimulatorStatus.
John
now that you mentioned the constructor I thought of a better idea, it would be much simpler to actually pass the hole user control (usrctrlSimulator) as a parameter in the constructor of the csAlgorithm and I think that will solve my problems thank you very much for your time
Sergiu
@Sergiu: Please consider accepting this answer if it helps you solving your question. Thanks!
John