Reputation: 1340
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?
}
}
}
Upvotes: 3
Views: 439
Reputation: 146499
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];
}
}
Upvotes: 0
Reputation:
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();
}
}
Upvotes: 3
Reputation: 55563
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
.
Upvotes: 0