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]
?
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.
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.
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.
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.
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...
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:
To clearify:
Form1.cs
public partial class Form1 { /* ... */ }
Form1.somepart.cs
public partial class Form1 { /* ... */ }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With