partial class


C

Chunekit Pong

why use partial class and define a class across couple different
source file like:

myapp1.cs
myapp2.cs
myapp3.cs

where
myapp1.cs has
namespace APP1
{
partial class mainForm
{
// ....
}
}

myapp2.cs has
namespace APP1
{
partial class mainForm
{
// ....
}
}

myapp3.cs has
namespace APP1
{
partial class mainForm
{
// ....
}
}

will it make code more messy?
 
Ad

Advertisements

J

Jeff Johnson

why use partial class and define a class across couple different
source file like:

It can be used to separate parts of your code that you might not want
"normal" programmers to touch. The classic example of this is the code that
Visual Studio 2005 and later auto-generates when you design a form visually.
All of this code is placed in <className>.Designer.cs and your form class is
now marked partial. This is because VS doesn't want you messing around with
the GUI code (for various reasons) and it helps unclutter your main source,
which should be concerned with making the GUI FUNCTION, not making it EXIST.

I've never found a compelling reason to use a partial class in classes that
I have coded 100% myself. Perhaps others have.
 
T

Tom Shelton

It can be used to separate parts of your code that you might not want
"normal" programmers to touch. The classic example of this is the code that
Visual Studio 2005 and later auto-generates when you design a form visually.
All of this code is placed in <className>.Designer.cs and your form class is
now marked partial. This is because VS doesn't want you messing around with
the GUI code (for various reasons) and it helps unclutter your main source,
which should be concerned with making the GUI FUNCTION, not making it EXIST.

I've never found a compelling reason to use a partial class in classes that
I have coded 100% myself. Perhaps others have.

The only other reason that I've used them for - beyond auto-generated code -
is when working in a team environment, where multiple people were working on
the different methods of the same class. And, then only because we were using
that abomination known as Visual Source Safe for source control. Now that we
have moved to svn - well, most of those partial classes have been or are being
put back together again :)
 
F

Family Tree Mike

Chunekit Pong said:
why use partial class and define a class across couple different
source file like:

myapp1.cs
myapp2.cs
myapp3.cs

where
myapp1.cs has
namespace APP1
{
partial class mainForm
{
// ....
}
}

myapp2.cs has
namespace APP1
{
partial class mainForm
{
// ....
}
}

myapp3.cs has
namespace APP1
{
partial class mainForm
{
// ....
}
}

will it make code more messy?

A useful place to go with partial classes is in a class that impliments
several interfaces. Each interface implimentation can be maintained
separately in individual partial class files.

Mike
 
T

TDC

Similarly, I've used them to isolate out COM interop Interfaces, such
that all the COM support is in a lesser-viewed partial class, and just
delegates to the .NET implementation as needed.
 
J

Josh Einstein

If I have a case where a class uses a couple nested classes, I often put the
nested classes in separate files.

File1.cs

public partial class MyObject {
... MyObject's implementation...
}

File2.cs

partial class MyObject {
private class MyNestedObject {
}
}

Josh Einstein
 
Ad

Advertisements

J

Jeff Johnson

If I have a case where a class uses a couple nested classes, I often put
the nested classes in separate files.

Actually I'm modifying some code I found online which has nested classes and
thanks to this discussion I broke them out exactly the same way last week!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top