Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

What are PARTIAL CLASSES and what are the scenarios I should be using them?

Can anyone please explain me what PARTIAL CLASSSES are?
In what scenarios we can use and when should we use them?
0
RajG1978
Asked:
RajG1978
2 Solutions
 
Miguel OzSoftware EngineerCommented:
Partial classes span the implemention over multiple files.
It can be used for:
Separate generated  code from your working code. E,g. Winforms, add functionality to web service imported classes.
Separate commonly-edited code from rarely-edited code. E.g. You have some code that does not change at all but you want to provide more functionality with your class.
In other words you can grow a class without using inheritance.
For further info check:
http://www.dotnetperls.com/partial
0
 
Jacques Bourgeois (James Burger)Commented:
Partial classes are classes whose code is distributed over more than one file.

Windows Forms used if for instance to separate the code written by the programmer and the code generated by the Forms Designer. When you design a form, the code generated ends up in a Partial Class FormName located in a FormName.designer.vb (or .cs in C#), while the code written by the programmer to react to events and makes the form work is also define in a Partial Class FormName contained in a second FormName.vb file. When you compile, the compiler treats the code in the 2 files / 2 partial classes as a single class.

Although it is not used often for other uses, one could thing for instance of a Photo class that is reused across many applications. Some applications use that class in relation with a database that gives information about the photos. Other applications do not care about the database and deal only with the data that comes straight from photo files on the hard disk.

One could like to split that class into 2 partial classes. The first partial contains the general code to work with photos, while the second one contains only the constructors, methods and properties that deal with the database aspect.

In projects that need to work with the database, you include the 2 files. In projects that do not need the database, you include only the main file. That way, you do not carry in the application the database code that would be useless.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now