views:

99

answers:

3

What is the best way to access components (e.g. imagelist, timer) from a form instance? I am working on multi form windows forms application on .NET Compact Framework version 3.5 SP1 with C#. I have a Controller class associated with each form for MVC implementation. Here is my sample Controller class.

public class Controller
{
      public void Init(Form f)
      {
            //f.Controls will allow access to all controls
            //How shall I access imagelist, timer on form f.
      }
}

My question is how can I access non visual components without taking a performance hit of reflection? Any code snippets are welcome. If reflection is only way, then can you provide me optimal way for components access please?

Thanks,

A: 

Create public property or public method.

adatapost
+2  A: 

You should be passing a strongly typed form or interface implementation which either exposes the controls directly (not the preferred choice) or abstracts the operations on the view into methods/properties which can be called from the controller (the preferred choice).

casperOne
A: 

No, reflection is not the way. In terms of simply accomplishing what you're going for, you can expose a read-only property that returns the control. For example, say you have an ImageList named imageList1 on your form:

public ImageList ImageList
{
    get { return imageList1; }
}

You can then access the ImageList property to get a reference to imageList1.

HOWEVER

This is smelly code. What you should expose is properties and functions that relate to what you need to DO with the ImageList. Your external code should not care about the particular implementation on your form (in other words, it should know you need to do something with images and provide functions to accomplish those actions; it shouldn't need to know that it's an ImageList control).

Adam Robinson