7 Replies - 775 Views - Last Post: 27 September 2014 - 02:40 AM Rate Topic: -----

#1 Damage   User is offline

  • Lord of Schwing
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,989
  • Joined: 05-June 08

Classes across multiple files

Posted 24 September 2014 - 02:07 PM

I didn't want to hijack a thread so i'm quoting from the old one

View PostCurtis Rutland, on 25 September 2014 - 08:47 AM, said:

Welcome to the modern programming world. We typically don't bother with massive, monolithic code files that get processed from top to bottom. In the Object Oriented world, code files don't mean much. In fact, in C#, I could have multiple classes defined in one file, or have one class split across several files.


why would you want to split up a class over multiple files?

Is This A Good Question/Topic? 0
  • +

Replies To: Classes across multiple files

#2 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Classes across multiple files

Posted 24 September 2014 - 02:39 PM

Good question. Time for a bit of a history lesson. C# 1.0 actually did not support this feature (called "Partial Classes", more on that later). All code for a class must be contained in the class, and could not be broken up over multiple files.

This wasn't seen as a problem. However, there was one traditionally messy area of code that desperately called out for some kind of separation: Form Designer code.

Form Designer code is the code generated by the WYSIWYG Form Editor. It's the stuff that you rarely need to edit by hand, and in fact can break your application if you make changes without knowing what you're doing. It's all the declaration, instantiation, and setting of properties of all the UI components like TextBoxes, Labels, etc...

In the original version of C#, all this form designer code was included in your Form's CS file, inside a collapsed #region. Only this #region would be overwritten when you made changes to the form in the designer.

But this is not ideal! You've got programmer-managed code side by side with tool-generated code. It makes for longer files, and it puts code that you usually shouldn't be messing with right under your nose.

Along comes "Partial Classes" in C# 2.0. This feature allows us to define parts of the same class in different files. When the code is compiled, the code from all partial classes bearing the same fully qualified name (namespace + class name; ex FileInfo vs System.IO.FileInfo) is merged into a single class.

This is great for the Form Designer. Now all the form code goes in Form.designer.cs instead of Form.cs. This keeps the auto-generated code separate from your code, and makes it less dangerous for the tool to re-generate the code and not accidentally wipe out your code.

That was the first major benefit people found. Another benefit is when using "ORM" software (such as LINQ to SQL or Entity Framework). The classes generated from the database tables are generated as partial classes. This allows you to extend one or more of these classes without losing your work if you need to regenerate the model from the database. For instance, if I want to add a read-only property to an EntityFramework-generated class, I can do it in a separate file, one that won't be overwritten if I need to regenerate my mappings.

But don't worry if you never take advantage of this feature. It's niche; it has limited utility outside dealing with generated code. Even Wikipedia's article suggests that its "primarily [useful] when parts of a class are generated automatically".

Hope that explains things!
Was This Post Helpful? 3
  • +
  • -

#3 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5509
  • View blog
  • Posts: 14,497
  • Joined: 18-April 07

Re: Classes across multiple files

Posted 24 September 2014 - 02:40 PM

Multiple reasons really including:

1) Separating the user interface from any logic of a class. Or simply cut down a large class into more manageable pieces. Do you need to worry about methods for network communications when you are worried about string manipulation methods? Probably not.
2) You can add to a class by creating the additional functionality in a new file without having to touch the original file. Maybe the two files are automatically generated.
3) You can have two developers work on the same class without having to worry about overwriting one another

Now do you find yourself using multiple files for a class often? I would say no. The only situations I would run into this is for extremely large projects and classes. But with classes if you are looking to break across multiple files, it probably means your class could just be broken into additional classes (decomposition). But never-the-less it is provided for you in case you need it.

Want to know more? search for the term "partial classes"

:)
Was This Post Helpful? 3
  • +
  • -

#4 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Classes across multiple files

Posted 24 September 2014 - 02:43 PM

View PostMartyr2, on 24 September 2014 - 04:40 PM, said:

Want to know more?

:)


Posted Image
Was This Post Helpful? 0
  • +
  • -

#5 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5509
  • View blog
  • Posts: 14,497
  • Joined: 18-April 07

Re: Classes across multiple files

Posted 24 September 2014 - 02:47 PM

I am sure that if an alien race came to take over Earth and we were in a war with them, we would be in the labs making these videos for the troops. DIC TV!

This post has been edited by Martyr2: 24 September 2014 - 03:05 PM

Was This Post Helpful? 0
  • +
  • -

#6 Damage   User is offline

  • Lord of Schwing
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,989
  • Joined: 05-June 08

Re: Classes across multiple files

Posted 24 September 2014 - 03:00 PM

Awesome, thank you
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7187
  • View blog
  • Posts: 24,356
  • Joined: 05-May 12

Re: Classes across multiple files

Posted 24 September 2014 - 07:52 PM

I personally hate #region's.

For me, the two times I usually use partial classes are when I am:
1. Writing methods for static classes like extension methods and PInvoke declarations.
I try to just use one class name for me extension methods (i.e. static class MyHelperExtensions { ... }) or PInvoke declarations (i.e. static class NativeMethods { ... }). Although I have only a single class name, I try to use multiple files where each file has common themes, or related functionality in them. (ex. MyHelperExtensions.String.cs, MyHelpExtensions.DateTime.cs, NativeMethods.Console.cs, NativeMethods.TaskScheduler.cs, etc.)

2. Generating code using templating.
I'm a big fan of T4. There are cases when I need to generate classes in a data driven manner, but some of the classes may have extra functionality that is better written as code rather than encoded in data. So, I use templates to generate partial classes, and then for those classes that need the extra support, I have extra code files for those. For example, I may have a base class Card, and a data file that lists cards by rank (Ace through King) and suit (Clubs through Spades) including jokers (red joker and black joker). The template would generate partial classes like partial class FiveOfDiamonds { ... }. When I need special functionality for a card (ex. jack of clubs), I would also have a separate file named JackOfClubs.cs that contains partial class JackOfClubs { ... } that contains the code specific to that card.
Was This Post Helpful? 1
  • +
  • -

#8 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6536
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Classes across multiple files

Posted 27 September 2014 - 02:40 AM

Personally I use #regions a lot. A common keystroke for me is control-m, control-o to contract the file. Then I can quickly open... #properties, #command-handlers, #save

I find they are even more helpful in XAML because it gets so damned long.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1