How to use separate .cs files in C#?

后端 未结 7 475
没有蜡笔的小新
没有蜡笔的小新 2021-02-02 00:55

Forum; I am a newbie working out a bit of code. I would like to know the best way to use separate .cs files containing other classes and functions. As an example of a basic func

7条回答
  •  说谎
    说谎 (楼主)
    2021-02-02 01:27

    The way most .net programmers would do it is:

    • Leave all your code inside MainForm.cs, but declare a new method for separating the code that does the clearing.

    I always leave in the event handler method (the one VS generates when you double-click the button) code to change the mouse cursor (in case the code I'm calling takes a couple of seconds or more to run) and catch all unhandled exceptions, and put my logic in a separate private method:

    partial class MainForm : Form // this is MainForm.cs
    {
        // This is the method VS generates when you double-click the button
        private void clearButton_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            try
            {
                ClearForm();
            }
            catch(Exception ex)
            {
                // ... have here code to log the exception to a file 
                // and/or showing a message box to the user
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
    
        private void ClearForm()
        {
            // clear all your controls here
            myTextBox.Clear();
            myComboBox.SelectedIndex = 0;
            // etc...
        }
    }
    

    In my opinion, if you want to follow the conventional way of doing things in .net, you should stick with this first example.

    Moving code out of the form .cs files is recommended when the code is not part of the form logic, for example, data access code or business logic. It this case I don't think that clearing the form controls qualifies as business logic. It is just part of the form code and should stay in the MainForm.cs.

    But if you really want to put the ClearForm method in another .cs file, you could create a new class and move the ClearForm method there. You would also need to change the Modifiers property of each control to Public so your new class can have access to them from outside MainForm. Something like this:

    public class FormCleaner // this is FormCleaner.cs
    {
        public void ClearForm(MainForm form)
        {
            // clear all your controls here
            form.myTextBox.Clear();
            form.myComboBox.SelectedIndex = 0;
            // etc...
        }
    }
    

    You would have to change the main form code to:

    partial class MainForm : Form // this is MainForm.cs
    {
        // This is the method VS generates when you double-click the button
        private void clearButton_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            try
            {
                FormCleaner cleaner = new FormCleaner();
                cleaner.ClearForm(this);
            }
            catch(Exception ex)
            {
                // ... have here code to log the exception to a file 
                // and/or showing a message box to the user
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }
    }
    

    But note that your FormCleaner class would have to know about your MainForm class in order to know how to clear each of the controls of the form, or you would need to come up with a generic algorithm that is able to loop through the Controls collection of any form to clean them:

    public class FormCleaner // this is FormCleaner.cs
    {
        // 'generic' form cleaner
        public void ClearForm(Form form)
        {
            foreach(Control control on form.Controls)
            {
                // this will probably not work ok, because also
                // static controls like Labels will have their text removed.
                control.Text = "";
            }
        }
    }
    

    And as others have said, MainForm.Designer.cs is machine-generated and you should never put your own code there.

提交回复
热议问题