thim24
thim24

Reputation: 634

C# .NET running code from an external file

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.

Upvotes: 1

Views: 295

Answers (1)

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391326

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.

Upvotes: 3

Related Questions