主类的访问变量 [英] access variable of main class

查看:94
本文介绍了主类的访问变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

File1.cs

File1.cs

namespace TestApp
{
    public partial class MainWindow :Window 
    {    
    }
}



File2.cs



File2.cs

namespace TestApp
{
    public partial class MainWindow
    {
        A ob = new A();
    }

    public class A
    {}
    public class B
    {
    }
}




对file2.cs的新修改




new modification to file2.cs

namespace TestApp
{
    public partial class MainWindow
    {
        init()
        {
            Exchange ob = new Exchange();
            ob.set_tb(textbox1);
        }
    }

    public class A
    {}
    public class B
    {}
    
    public class Exchange : Window
    {
        TextBox tb1;
        
   // some hack to get textbox access
        public void set_tb(TextBox tcontrol)
        {
            tb1=tcontrol;
        }
    }

}



同样,我必须在Exchange类中添加很多功能,这些功能使用了主类中提供的许多变量.

在主类中上交换类是一个好选择吗?



similarly there are lot of function i have to add in Exchange class which uses lots of variable available in main class.

is it a good option to take exchange class in main class.

推荐答案

没有完全按照说明进行操作,但根据我的一般经验,避免在UI类之间传输UI对象.而是考虑在Windows上使用属性和方法来传输所需的信息并执行功能.如果您可以将UI与数据分离,将逻辑从UI分离到单独的类,则IMO会更好.这样,程序的可维护性和鲁棒性会更好.
Didn''t quite follow the description, but as a general rule of thumb I''d avoid transferring UI objects between UI classes. Instead consider using properties and methods on your windows to transfer just needed information and to execute functionality. IMO even better if you could separate the UI from the data and from the logic to separate classes. This way the maintainability and robustness of the program would be better.


这篇关于主类的访问变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆