posted on Tuesday, December 20, 2005 10:57 PM
by
BayerWhite
Partial To Partial Classes
I am trying to figure out how I did it without them! Partial classes make it so easy to clean up defined objects or classes taking advantage of VS2005 intellisense. Now for most, I think we enjoy keeping all of our classes intact, however partial classes help class designers keep their defined class members together. So what do I mean? Let's say you have designed classes that contain many properties, and for whatever reason these properties cannot be inherited from other class objects. Now let's say this dilemma not only applies to properties, but functions and methods too. Partial classes remedy this by allowing classes to be broken up into different physical files, and then at compile time, the .Net Framework compiles all of the partial classes as ONE. Let's look at some code so we can better understand what is happening here.
I have three class files…
1. PersonFunctions.cs
public partial class Person
{
public string PersonFeels()
{
string strFeeling = PersonsName+" is "+PersonsAge.ToString()+" and feels ";
if (PersonsAge >= 80)
strFeeling += "Old!";
else if (PersonsAge < 80 && PersonsAge > 50)
strFeeling += "tired of technology changing!";
else
strFeeling += "just wonderful!";
return strFeeling;
}
}
2. PersonMethods.cs
public partial class Person
{
public void Birthday()
{
_Age+=1;
}
}
3. PersonProperties.cs
public partial class Person
{
int _Age = 0;
string _Name = "";
public int PersonsAge
{
get
{
return _Age;
}
set
{
_Age = value;
}
}
public string PersonsName
{
get
{
return _Name;
}
set
{
_Name = value;
}
}
public Person()
{
}
public Person(string Name, int Age)
{
PersonsAge = Age;
PersonsName = Name;
}
}
As you can see, there are three different files being used to build a Person object. Try putting this code in your ASP.net page to for a demo!
protected
void Page_Load(object sender, EventArgs e)
{
Person SiteUser =
new Person("Bayer",55);
SiteUser.Birthday();
Response.Write(SiteUser.PersonFeels());
}
Partial classes will allow your class files to be cleaner and easier to understand, because these files can be broken out into multiple files, simply by separating like object members. This also increases productivity by utilizing more than one developer to work on the partial class files.