Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add partial file to existing form file

Tags:

c#

partial

I have a Window Form App project. At the moment all of my code is in Form1.cs file which is the default file. Now I have about 1300 lines of code in this single file. I want to break down this one file code into several files and I want to use the "partial" key word (I don't want to do anything drastic). So how should I add the files

Right click project name->add->new item ->class results into class1.cs, class2.cs and so on

But this file converts to a form form file after compilation. What's the correct way of adding so that the new file integrates with my existing project Form1.cs and Form1.cs[Design]?

like image 292
user1903439 Avatar asked Feb 19 '13 08:02

user1903439


People also ask

How do I add a partial class in Visual Studio?

Open Visual Studio from "Start" -> "All programs" -> "Microsoft Visual Studio". Then, go to "File" -> "New" -> "Project..." then select "Visual C#" -> "Windows" -> "Console application". Then, specify the name such as Partial class or whatever name you wish and the location of the project and click on the "OK" button.

Can we create object of partial class?

All parts of a partial class should be in the same namespace. Each part of a partial class should be in the same assembly or DLL, in other words you can't create a partial class in source files of a different class library project. Each part of a partial class has the same accessibility.

What is partial modifier in C#?

The partial keyword indicates that other parts of the class, struct, or interface can be defined in the namespace. All the parts must use the partial keyword. All the parts must be available at compile time to form the final type. All the parts must have the same accessibility, such as public , private , and so on.

What is partial class in VB net?

Partial classes can be used to split the definition of classes, structures, and interfaces, which supports simultaneous development or the separation of generated from user-generated code. Each part of a partial class must be available at compile time.


2 Answers

You have to keep the namespace, the class name and mark it with partial. The file name is not really important for it to work, but it's a good practice so that the developers can identify rapidly the contents of the file.

Form1.cs

namespace TheSameNamespace
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }

    // other definitions
}

Form1.Designer.cs

namespace TheSameNamespace
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        // the rest of the designer class
    }
}

Form1.Calculations.cs

namespace TheSameNamespace
{
    partial class Form1
    {
        // calculation methods definitions
    }
}

Form1.EventHandlers.cs

namespace TheSameNamespace
{
    partial class Form1
    {
        // event handlers definitions
    }
}

and so on...

like image 73
Alex Filipovici Avatar answered Sep 20 '22 05:09

Alex Filipovici


The partial keyword is primarly for generated files, which can be extended by your own code - there is no use in splitting a single bloated class into multiple partials, but if you really want to do it then you have to:

  1. Create a new class.
  2. Rename the class to match your own class (Form1.xxx.cs)
  3. Use the partial key-word and adjust the name and the namespace.

To clearify:

Form1.cs

public partial class Form1 { /* ... */ }

Form1.somepart.cs

public partial class Form1 { /* ... */ }
like image 43
TGlatzer Avatar answered Sep 24 '22 05:09

TGlatzer