Static Variables.

G

Gobinath

Hi,

I have a pretty basic question.

I have a C# control with lots of classes and I need few instances(manager
classes) shared by all the classes in the conrol. So I define a Static Class
and it works OK.

Now When I want multiple instances of the control in the same application
and each instance to have it own set of manager instances, how do I do it?
Static is shared between the two instance of the control, which I want to
avoid.

Is there a specific variable type or a standard design pattern for it?

Thanks & Regards,
Gobi.
 
R

Roman Wagner

I asume that you add instances of your control with the designer.
Try something linke that.

ClassThatUseMyControl {

...
public ClassThatUseMyControl() {
InitializeComponent();
_MyClassInstance1.Controller =
MyClassControllerFactory.GetController();
_MyClassInstance2.Controller =
MyClassControllerFactory.GetController();
}
}

public class MyControl : Control {

...

public IMyController {
get { return _myController; }
set { _myController = value; }
}
}

public interface IMyController {
//extract interface from the current implementation of your
controller
}

public static class MyControllerFactory {

private class MyPrivateController : IMyController {
//your controller code
}

public IMyController GetController() {
return new MyPrivateController();
}
}
 
J

James

Hi,

I think your best bet will be to "promote" your static manager class to an
instance variable on the control.

James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top