views:

143

answers:

5

Hi everyone. I've gotten to a point where my main code file is about a thousand lines long and it's getting un-manageable; that is, I'm starting to get confused and not know where to locate some things. It's well-commented but there's just too much stuff.

I'd really like to be able to organize my code into different files, each with its own purpose. I want to get all the help VS gives me as I type when I edit these other files. A picture can say a thousand words:

alt text

Is what I'm trying to do even possible?

A: 

Yes you can split any partial class across as many files as you like.

Ben Robinson
A: 

Try using public void. Thats all I know. sorry!

clone1018
+4  A: 

Yes, but you need to be in the same namespace and declare the class just like you did in the main file, an example:

file1.cs

namespace Names
{
    public partial class Hello
    {
        public void DoSomething() { }
    }
}

file2.cs

namespace Names
{
    public partial class Hello
    {
        public void Go() { DoSomething(); }
    }
}
BrunoLM
I originally split my WinForms project up like this, taking advantage of partial classes. I'd make a "utility" file and a "parsers" file, etc. The one headache was that when splitting code for a form class, each partial class tried to open up as a new, blank form in the Visual Studio IDE unless I specified I wanted the code view. In any case, I no longer do this, I've found that making separate classes help substantially!
JYelton
+7  A: 

Although what other people say about partial classes is true. I'd suggest you also analyze refactoring opportunities on your class.

If you're having problems to manage it, you could try to split your single class in several classes with less responsibilities.

IMHO partial classes may not help very much. Do you have your class separated in regions? Regions improve the readability of your code.

Claudio Redi
That sounds like a comment not an answer
BrunoLM
I agree. If a class is becoming large and hard to manage, you should consider refactoring it into multiple smaller classes. That will naturally organize it into smaller files as well.
C. Dragon 76
@BrunoLM: Why do you think that? I'm giving him a different point of view since I don't think partial classes is the best solution for his problem.
Claudio Redi
@Bruno, no, it is sound advice.
Anthony Pegram
Ok, whatever, I just don't see it as an answer, IMO it should be posted as a comment. The question was how to separate the code in other files.
BrunoLM
Refactoring the class into multiple classes *is* separating the code into multiple files.
Robert Rossney
@Robert: I think you know that's not true :-) OOP it's not about separating the code into multiple files
Claudio Redi
I appreciate your input - right now unfortunately, I don't see a lot of ways to split it up logically into lots of different classes. The problem is my windows has around 200-300 controls, all of which get manipulated at runtime. I really do not want to pass all of those by reference to another class just for that class to work with them.
Adam S
@Adam S: You might consider creating `UserControls` to logically divide your 200-300 controls into managable chunks, or perhaps using more than one form for your interface... You seem to be falling into the technical debt trap - the longer you go without refactoring the more difficult it will be to maintain and modify your code. This can escalate to the point where it costs too much in time and effort to add new features.
Phil
A: 
  1. Strip out each decent size class into at least one file. Wrap each class in the same namespace.
  2. For large classes use either:
    a. Region blocks eg

    #region // Members
    int my_int;
    // other members...
    #endregion

    b. partial keyword to break a single class accross several files.

Ricibob