1

I'm pretty new with C#, and I was wondering if it would be possible to move parts of code (from my main Form) to an external .cs file (still in the same solution), with the same level of access to variables and functions from my form1.cs file, as my original form1.cs file.

I'm working with the latest version of the .NET-framework and I have no other references to (external) code.

Here is a simple example of what I'm looking for:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Code1
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        
        // variables
        text1 = "Hello"
        
        public void ExternalCode()
        {
            this.BackgroundImage = null;
            Btn1.Text = "Click me";
            Lbl1.Text = text1;
        }

        private void Btn1_Click(object sender, EventArgs e)
        {
            ExternalCode();
        }
    }
}

This form only has a button named "Btn1" and a label named "Lbl1" in it.

I would like the code inside the method SeparateCode to be in a separate file, but still with the ability to access the variables and controls from Form3, and for Form3 to still be able to execute the method in the external file (ExternalCode).

I thought of making this file a daughter of the Form3.cs, but as I said, I'm still pretty new at this so I don't really know how to.

2
  • 2
    Yes, you can do that, and the reason you can do that is the partial keyword there. So create a separate .cs file, that uses the same namespace, and also has public partial class Form3 (you don't need to specify that it inherits from Form, as that has already been done), and then move your SeparateCode method to this new file. The compiler will treat these two files as "the same class" and compile them as though you had all the code in just one file. Commented Jun 6, 2020 at 23:28
  • Thanks Lasse! Gonna try that out rn. Helping me a lot Commented Jun 6, 2020 at 23:30

1 Answer 1

3

Yes, that is possible, because the class has been declared as partial, here:

       v-----v
public partial class Form3 : Form

This keyword means that the file you grabbed that code from can be one of many, all declaring their own partial content for this class.

So what you can do is add yet another file to your project, and make sure it contributes to this class. To do that you must:

  1. Declare the class inside the file as belonging to the same namespace as the original
  2. Declare the class inside to be the same class as the original

Note that you don't have to mention inheritance, such as the part with : Form from your original, as this will still be understood from your original file.

So, if you add a file with this content:

namespace Code1                        // <-- this has to be the same
{
    public partial class Form3         // <-- as does this
    {
        public void ExternalCode()
        {
            this.BackgroundImage = null;
            Btn1.Text = "Click me";
            Lbl1.Text = text1;
        }
    }
}

then you can remove ExternalCode from your original file, and it should still work just fine.

The compiler will treat all of these files as building up the same, one, class, Form3.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.