Solved

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

Posted on 2014-01-29
2
223 Views
Last Modified: 2014-01-30
Can anyone please explain me what PARTIAL CLASSSES are?
In what scenarios we can use and when should we use them?
0
Comment
Question by:RajG1978
2 Comments
 
LVL 35

Assisted Solution

by:Miguel Oz
Miguel Oz earned 250 total points
ID: 39819666
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
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 250 total points
ID: 39819968
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delete HTML table rows 12 29
Modal Popup Extender control 1 20
VB.Net - MemoryMappedFiles - Confirm receipt 2 19
VB.NET 2008 Winforms Signing 13 17
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question