C #: How do you access an instance of an object?

I created a splash class with a timer on it, and when the timer is finished, it calls another class, as shown in the code below. When I 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?
        }
    }
}
+3
source share
3 answers

CustomEventWatcher will require a reference to MainWindow, for example, through a property in 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();
    }
}
+3
source

You should have MainWindowboth an instance or a static variable, not a method variable. This will allow access to them from several classes, if marked as public.

0
source

factory (, ).

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, Splash: , , .

{

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];
    } 
} 
0

Source: https://habr.com/ru/post/1763783/


All Articles