Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:如何访问对象实例?_C# - Fatal编程技术网

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

C#:如何访问对象实例?,c#,C#,我创建了一个带有计时器的splash类,当计时器完成时,它会引用另一个类,如下面的代码所示。当我创建一个新类时,如何访问MainWindow namespace Kinetics { public class KineticsCommand : RMA.Rhino.MRhinoCommand { Splash Splash = new Splash(); Splash.Show(); } public partial class Sp

我创建了一个带有计时器的splash类,当计时器完成时,它会引用另一个类,如下面的代码所示。当我创建一个新类时,如何访问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?
        }
    }
}

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

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

新编辑版本,带单例:

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

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

但是覆盖不支持比前3个参数更多的参数,那么我该怎么办?如果这是您的代码,您可以添加另一个覆盖吗?如果不是,请将启动屏幕设置为具有工厂的单例(请参阅我编辑的版本)我还建议您使用MainUIInterface,否则它会让您感觉到类之间的依赖关系,假设明天您想显示另一个窗口,您唯一需要做的就是在新窗口中实现此接口。。。