Is it possible to split the code into several partial files?

I have a .aspx web form with .aspx.cs code. The code is nearly 2,000 lines long, and it reaches the point where the only way to easily navigate it is to put a ton of spaces between sections, zoom out so that I can see the physical appearance of the code and then zoom in where I want to edit. In other words, this is a big pain. I would like to split this line 2000 into different files, which are concepts in the code. Therefore, when I need to change some functions on the jQuery tab “Employee” on the page, I can just go to a partial class that contains only the “Employee” functionality.

In the "New item" menu, I can not find anything for the additional file with the code, and not the .aspx.cs file. I tried to create a .cs file by renaming it to .aspx.cs and assigning it the same partial class name --- no go, I did not see any of the methods of the incomplete class by default, nor the controls on page

I understand the concept that if your “class”, in the sense of OOP, takes so long, it does too much. What I don't understand is a “class” in terms of code for a web form. A form cannot actually be broken down into smaller forms. User experience should be carried out on one page.

+7
source share
4 answers

You can use partial classes . Therefore, add a new .cs file to the project and use an incomplete class.

For example, suppose you have the following Default.aspx.cs :

 public partial class _Default : System.Web.UI.Page { ... some 2000 lines of code } 

you could add another .cs file to your project in which:

 public partial class _Default { ... a subset of the 2000 lines of code could be moved here in order to isolate some functionality } 

It is very important that this partial class is declared in the exact same namespace as the original class and has the same name.

But note that using partial classes only hides shit. He does not cleanse it. If you want to clear it, you will have to consider OOP methods for separating functionality in different classes. You must lay down your application.

+13
source

You can divide classes into separate files using partial classes - see

http://msdn.microsoft.com/en-us/library/wa80x488%28v=vs.80%29.aspx

However, 2,000 lines look large enough for your code. Have you considered the possibility of extracting functionality into different helper classes / strategies adapted to specific functions? So, instead of creating a new partial class for the Employee functionality, create a new class with verifiable auxiliary and logical functions and add a link to them from your source class?

+1
source

Try

 Ctrl-M, Ctrl-O 

followed by

 Ctrl-M, Ctrl-L 
+1
source

Use or partial classes solve the general problem you are studying. There is another way to manage your code using region tags in your code so that you can open and close areas of the code.

Example:

  #region isntances public StreamWriter LogFile; public FileStream fs; string _RunID = Guid.NewGuid().ToString(); #endregion 

This will allow you to tag the code to better track it.

Hope that helps

0
source

All Articles