tags:

views:

35

answers:

2

Thank you for your help in advance.

In the following code located in Main(): Application.Run(new frmBackground(frmExit)) I am trying to launch window frmBackground that takes a window as a parameter in the constructor and after all content is loaded (background image), it then launches the passed window. This however does not compile and only compiles when I use Application.Run(new frmBackground(new frmExit())) which passess the correct window parameter, but on its own, creates an instance of frmExit and launches the window even when frmBackground code that launches the window is commented out.

Thank you again.

A: 

From what I understand, when you say Application.Run(new frmBackground(frmExit)), you're not passing an instance of frmExit, but the type (the class), and from what I understand, your method is expecting an instance... you might want to do something like:

frmExit exitForm = new frmExit();
Application.Run(new frmBackground(exitForm));

Or have some sort of "bag" class where you keep the reference of some resources you know you might need, like the reference for this form (frmExit), and change the constructor of frmBackground, then you replace the calls for the parameter variable with the values on your "bag" class.. something like that..

If this is not what you're trying to achieve, I'd suggest you give more details of your code here

Thiago Santos
the original Application.Run(new frmBackground(new frmExit)) does pass an instance of frmExit
Conrad Frix
Thank you for your response. I already tried this approach and it results in exactly the same behavior. I dont want to pass an instance of the window (it results in it's showing), just the window as a type to instantiate later. I hope I am not too confusing.
Daniel
Then you're using the wrong signature on the constructor of frmBackground... and if you just want to instantiate later, why not create a property or method where you can pass a type (say frmExit, and for that you must enforce that this type must inherit from the same type that frmExit does) and then apply a factory to create an instance of that type?
Thiago Santos
I would like frmBackground to accept a window type without specifing any explicit reference to frmExit as frmBackground will be used among many projects most of which do not KNOW frmExit. I tried a property approach in frmBackground but cannot get it behave properly. Would you be able to provide a snippet of code that explains your approach. Cheers!frmBackground:public Window w{ set { value = new Window(); value.ShowDialog(); }}Main:frmBackground w = new frmBackground();Application.Run(w);w.w = (type)frmExit;
Daniel
A: 

pass the Type into the constuctor and then use reflection to create it in your frmBackground

This will show you how

http://stackoverflow.com/questions/1751123/create-an-instance-of-a-type-provided-as-a-parameter-to-a-method

Conrad Frix