简体   繁体   English

C#:如何访问对象实例?

[英]C#: How do you access an object instance?

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. 我创建了一个带有计时器的splash类,当计时器完成时,它将实例化另一个类,如下面的代码所示。 When i then create a new class how can i access MainWindow? 当我创建一个新类时,如何访问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?
        }
    }
}

CustomEventWatcher will need a reference to MainWindow, for example via a property within CustomEventWatcher: CustomEventWatcher需要对MainWindow的引用,例如通过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();
    }
}

You need to have MainWindow as an instance or static variable, not a method variable. 您需要将MainWindow作为实例或静态变量,而不是方法变量。 This will allow it to be accessed from multiple classes, as long as its marked as public . 这将允许从多个类访问它,只要它标记为public

use a static factory method or property (Property shown below). 使用静态工厂方法或属性(如下所示)。

New EDITED VERSION w/SIngleton: 新的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. 旧版本,使用Splash变量:无论您在何处调用该方法,都将该变量传递给它。

namespace Kinetics { 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];
    } 
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM