全局窗口窗体与。本地或私人窗口表格

发布于 2024-11-27 19:34:17 字数 820 浏览 1 评论 0原文

自从我开始阅读 .NET 的各种语言并进行编程以来,我被告知程序中任何内容的“全局”都是非常非常糟糕的。整个想法对我来说似乎根本没有任何意义。在过去 10 到 13 年里,我一直是一名程序员/软件开发人员,我开始接受这样一个事实:有时,作为一名程序员,您无法在不使用全局对象或变量的情况下只进行编码。

确实,全局变量或对象可能会导致程序中出现错误,但这并不意味着它是由编程语言或编译器本身造成的,而是由程序员自己造成的。

就全局窗口窗体而言,当您无法将窗体实例作为参数传递时,您如何期望能够在事件中以编程方式从程序的其他部分更改“相同”窗口窗体属性。如果窗口窗体不是全局的,则每次程序需要显示或更改其属性时,都必须在本地实例化一个新窗体,但新窗体将与您打开的第一个窗体不同。如果表单在私有、受保护或公共部分下的类中声明,则需要实例化该类,然后需要实例化表单本身并在需要的地方传递。我确信您在执行此操作时会遇到其他一些编程问题。

我什至不知道为什么他们会改变 CLOSE 方法的真正定义。关闭意味着关闭而不是处置。

我并不是想否认 Global 很糟糕的整个想法。我只是想了解为什么以及以什么方式。

有人可以解释并给我一个例子,说明如何在没有单个“全局”窗口表单甚至变量的情况下完全编程吗?假设一个程序有两个窗口形式。一个是主窗体,另一个窗体有一个 Ttimer 和一个标签。主窗体有一个 Tbutton 和单击事件,可打开第二个窗体。第二种形式的计时器有一个 Tick 事件,每秒将当前日期和时间设置为其标签文本属性。当您单击 TButton 时,它应该打开第二个窗体并每秒显示当前日期和时间。现在向我展示我在 C# 或 Delphi XE 或 prism 或 C++ 或 VB 中描述的上述程序的代码,没有任何全局变量。

谢谢,

Since I started to read up and program in various languages for .NET, I have been told that "Global" of anything in your program is very very bad. That whole idea doesn't seem to make any sense at all to me. Having been a programmer/software developer for the last 10 to 13 years, I come to accept the fact that sometimes you as a programmer cannot just code without ever using a global object(s) or variable(s).

It is true that Global variable(s) or Object(s) can lead to bug(s) in your program, but that does not mean it is due to the programming language or the compiler itself but the programmer himself or herself.

As far as the global window forms go, how do you expect to be able to change the "SAME" window form properties programmatically from other parts of your program within an event when you can't pass in the instance of the form as a parameter. If a window form is not global, one has to instantiate a new form locally every time their program needs to display or change its properties, but then the new form won't be the same as the first form you opened. If the form is declared within a class under private, protected or public section, then the class needs to be instantiated, then the form itself needs be instantiated and passed around wherever it is needed. I am sure you will run into some other programming issues in doing that.

I don't even know why they would even change the true definition of the CLOSE method. Close means close not dispose.

I am not trying to put down the whole idea of Global being bad. I am just trying to understand why and in what way.

Can someone explain and give me an example of how one would program completely without a single "GLOBAL" window(s) form or even variable(s)? Let's say a program has two window forms. One is the main form and the other form has a Ttimer and a label. Main form has a Tbutton and click event which opens the second form. Second form Timer has a Tick event that sets the current date and time to it's label text properties every second. When you click on the TButton, it should open the second form and display the current date and time every second. Now show me the code for the above program I described in C# or Delphi XE or prism or C++ or VB WITHOUT ANY GLOBAL VARIABLE(S).

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相思故 2024-12-04 19:34:18

这是一个非常基本的版本,没有全局计时器表单(没有用于处理等或取消注册事件的代码,但您明白了):

主表单:

public partial class MainForm : Form
{
    // local reference to the timer form
    private readonly TimerForm _timerForm;

    public MainForm()
    {
        InitializeComponent();

        // just create it once
        _timerForm = new TimerForm();
    }

    private void btnShowDateTime_Click(object sender, EventArgs e)
    {
        // show the form, no need to instantiate it
        _timerForm.Show();
    }
}

计时器表单:

public partial class TimerForm : Form
{
    private Timer _timer = new Timer();

    public TimerForm()
    {
        InitializeComponent();
        _timer.Interval = 1000;
        _timer.Tick += _timer_Tick;
        _timer.Start();
    }

    void _timer_Tick(object sender, EventArgs e)
    {
        lblDateTime.Text = DateTime.Now.ToString();
        lblDateTime.Refresh();
    }

    private void btnHide_Click(object sender, EventArgs e)
    {
        // hide the form rather than closing it
        this.Hide();
    }
}

Program.cs:

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainForm());
    }
}

输出(除了这个简单示例的美观之外):

在此处输入图像描述

现在,如果您关闭计时器表单然后尝试显示它,那么这会中断,但您需要编写代码对于这种情况,如果需要,重新创建表单(或者在主表单关闭之前不允许关闭表单)。

Here is a very basic version without a global timer form (no code for disposing etc. or unregistering events, but you get the idea):

Main Form:

public partial class MainForm : Form
{
    // local reference to the timer form
    private readonly TimerForm _timerForm;

    public MainForm()
    {
        InitializeComponent();

        // just create it once
        _timerForm = new TimerForm();
    }

    private void btnShowDateTime_Click(object sender, EventArgs e)
    {
        // show the form, no need to instantiate it
        _timerForm.Show();
    }
}

Timer Form:

public partial class TimerForm : Form
{
    private Timer _timer = new Timer();

    public TimerForm()
    {
        InitializeComponent();
        _timer.Interval = 1000;
        _timer.Tick += _timer_Tick;
        _timer.Start();
    }

    void _timer_Tick(object sender, EventArgs e)
    {
        lblDateTime.Text = DateTime.Now.ToString();
        lblDateTime.Refresh();
    }

    private void btnHide_Click(object sender, EventArgs e)
    {
        // hide the form rather than closing it
        this.Hide();
    }
}

Program.cs:

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainForm());
    }
}

The output (beauty aside for this quick example):

enter image description here

Now this would break if you closed the timer form and then tried to show it, but you'd code for that scenario, recreating the form if needed (or not allowing the form to be closed until the main form was closing).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文