开发者

Global Window Form Vs. Local or private Window Form

开发者 https://www.devze.com 2023-03-25 17:35 出处:网络
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

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 for开发者_Go百科m 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,


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):

Global Window Form Vs. Local or private Window Form

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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号