tags:

views:

119

answers:

3

I created a splash class with a timer on it, and when the timer is finished it instances another class as shown in the code below. When i then create a new class how can i access MainWindow?

namespace Kinetics
{
    public class KineticsCommand : RMA.Rhino.MRhinoCommand
    {
       Splash Splash = new Splash();
       Splash.Show();
    }

    public partial class Splash : Form
    {
        public Splash()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Close();

            MainUI MainWindow = new MainUI();
            MainWindow.Show();
        }
    }

    public class CustomEventWatcher : MRhinoEventWatcher
    {
        public override void OnReplaceObject(ref MRhinoDoc doc, ref MRhinoObject old, ref MRhinoObject obj)
        {
           // How can i access the class from here?
        }
    }
}
A: 

You need to have MainWindow as an instance or static variable, not a method variable. This will allow it to be accessed from multiple classes, as long as its marked as public.

Richard J. Ross III
I'm a newbie, so could you tell me how this is done?
Bildsoe
Just use justin's method, it'll work for what you need.
Richard J. Ross III
A: 

use a static factory method or property (Property shown below).

New EDITED VERSION w/SIngleton:

namespace Kinetics {

public class KineticsCommand : RMA.Rhino.MRhinoCommand 
{ 
   Splash splashVariable= Splash.SingletonInstance; 
   splashVariable.Show(); 

   // or, combine and just write...

   Splash.SingletonInstance.Show();
} 

public partial class Splash : Form 
{ 
    private Splash splsh;
    private Splash() 
    { 
        InitializeComponent(); 
    } 
    public static Splash SingletonInstance  // Factory property
    {
        get { return splsh?? (splsh = new Splash()); }
    }

    //  Factory Method would be like this:
    public static Splash GetSingletonInstance()  // Factory method
    {
        return splsh?? (splsh = new Splash()); 
    }

    private void timer1_Tick(object sender, EventArgs e) 
    { 
        this.Close(); 

        MainUI MainWindow = new MainUI(); 
        MainWindow.Show(); 
    } 
} 

public class CustomEventWatcher : MRhinoEventWatcher 
{ 


    public override void OnReplaceObject(ref MRhinoDoc doc, 
                 ref MRhinoObject old, ref MRhinoObject obj) 
    { 
       // to access the instance of the class from here,  now 
       // all you need to do is call the static factory property
       // defined on the class itself.
       Splash.SingletonInstance.[Whatever];
    } 
} 

OLD Version, using the Splash variable: Wherever you call the method, pass that variable to it.

namespace Kinetics {

public class KineticsCommand : RMA.Rhino.MRhinoCommand 
{ 
   Splash splashVariable= new Splash(); 
   splashVariable.Show(); 
} 

public partial class Splash : Form 
{ 
    public Splash() 
    { 
        InitializeComponent(); 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
        this.Close(); 

        MainUI MainWindow = new MainUI(); 
        MainWindow.Show(); 
    } 
} 

public class CustomEventWatcher : MRhinoEventWatcher 
{ 


    public override void OnReplaceObject(ref MRhinoDoc doc, 
                 ref MRhinoObject old, ref MRhinoObject obj, 
                 Splash splashScreen) 
    { 
       // to access the instance of the class from here, 
       // pass in the variable that holds a reference to the instance
       splashScreen.[Whatever];
    } 
} 
Charles Bretana
But the override doesn't support more arguments than the first 3, what do i do then?
Bildsoe
If this is your code, can you add another override? if not, make your splash screen a singleton that has a factory (see my edited version)
Charles Bretana
+3  A: 

CustomEventWatcher will need a reference to MainWindow, for example via a property within CustomEventWatcher:

public class CustomEventWatcher : MRhinoEventWatcher
{
    MainUI _mainUI = null;
    public MainUI MainWindow { get { return _mainUI; } set { _mainUI = value; } }

    public override void OnReplaceObject(ref MRhinoDoc doc, ref MRhinoObject old, ref MRhinoObject obj)
    {
       if(_mainUI != null)
           _mainUI.Whatever();
    }
}

public partial class Splash : Form
{
    public Splash()
    {
        InitializeComponent();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        this.Close();

        MainUI MainWindow = new MainUI();
        CustomEventWatcher cew = new CustomEventWatcher();
        cew.MainWindow = MainWindow;
        MainWindow.Show();
    }
}
Justin Pinkley
I would also recommend you to use MainUIInterface otherwise it feels like you dependency between the class and lets say tomorrow you would like to show another window the only thing you would have to do would be implement this interface in you new window...
yoav.str