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

时间:2010-09-08 12:26:29

标签: c#

我创建了一个带有计时器的启动类,当计时器完成时,它将实例化另一个类,如下面的代码所示。当我创建一个新类时,如何访问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 个答案:

答案 0 :(得分:3)

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();
    }
}

答案 1 :(得分:0)

您需要将MainWindow作为实例或静态变量,而不是方法变量。这将允许从多个类访问它,只要它标记为public

答案 2 :(得分:0)

使用静态工厂方法或属性(如下所示的属性)。

新的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];
    } 
} 

旧版本,使用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];
    } 
}