Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to organize classes/methods in Visual Studio

How to organize classes/methods in Visual Studio

Scheduled Pinned Locked Moved C#
csharpvisual-studiotoolstutorialquestion
8 Posts 6 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    ssclaire
    wrote on last edited by
    #1

    I believe it is tradition/convention to organize source files in a project so each class is contained in one .cs source file. Correct? I am writing several static utility classes with about 10-20 methods each. And each will have about four (4) overloaded methods so the static class definition (and the .cs source file) is getting pretty lengthy. Should I keep to the one-class, one-source-file convention, or is there a better way to organize my project in the Solution Explorer?

    T C J realJSOPR A 6 Replies Last reply
    0
    • S ssclaire

      I believe it is tradition/convention to organize source files in a project so each class is contained in one .cs source file. Correct? I am writing several static utility classes with about 10-20 methods each. And each will have about four (4) overloaded methods so the static class definition (and the .cs source file) is getting pretty lengthy. Should I keep to the one-class, one-source-file convention, or is there a better way to organize my project in the Solution Explorer?

      T Offline
      T Offline
      TheCardinal
      wrote on last edited by
      #2

      you can try to chop it to small files and create a partial class

      Life - Dreams = Job TheCardinal CTC-RDG

      1 Reply Last reply
      0
      • S ssclaire

        I believe it is tradition/convention to organize source files in a project so each class is contained in one .cs source file. Correct? I am writing several static utility classes with about 10-20 methods each. And each will have about four (4) overloaded methods so the static class definition (and the .cs source file) is getting pretty lengthy. Should I keep to the one-class, one-source-file convention, or is there a better way to organize my project in the Solution Explorer?

        C Offline
        C Offline
        carbon_golem
        wrote on last edited by
        #3

        Yes, definitely a good idea to keep one class per file as good programming practice. Um... Tradition? No. We (those at my company) like to strictly adhere to this rule and leave the partial class definition to automatically generated code only. Actually using it is IMHO a bit unsavory to read but preferable to a single file mess. You can use the #region directive to collapse source code sections together for convenience. Hope that helps Scott P

        “It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration.” -Edsger Dijkstra

        realJSOPR 1 Reply Last reply
        0
        • S ssclaire

          I believe it is tradition/convention to organize source files in a project so each class is contained in one .cs source file. Correct? I am writing several static utility classes with about 10-20 methods each. And each will have about four (4) overloaded methods so the static class definition (and the .cs source file) is getting pretty lengthy. Should I keep to the one-class, one-source-file convention, or is there a better way to organize my project in the Solution Explorer?

          J Offline
          J Offline
          Joe Woodbury
          wrote on last edited by
          #4

          ssclaire wrote:

          I believe it is tradition/convention to organize source files in a project so each class is contained in one .cs source file. Correct?

          It all depends. I have files with one class and files with several classes and one class spread across multiple files. The key for me is to organize files in a logical, clear way. Having too many files can be as distracting and confusing as having few files with too much code in each.

          Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

          1 Reply Last reply
          0
          • C carbon_golem

            Yes, definitely a good idea to keep one class per file as good programming practice. Um... Tradition? No. We (those at my company) like to strictly adhere to this rule and leave the partial class definition to automatically generated code only. Actually using it is IMHO a bit unsavory to read but preferable to a single file mess. You can use the #region directive to collapse source code sections together for convenience. Hope that helps Scott P

            “It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration.” -Edsger Dijkstra

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #5

            Partial classes are great for segregating related functionality into discrete files. For instance, I only put generated code into the main form file, and anything added manually into a separate file(s). For instane, all code related to a BackgroundWorker object would go into a separate file.

            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

            1 Reply Last reply
            0
            • S ssclaire

              I believe it is tradition/convention to organize source files in a project so each class is contained in one .cs source file. Correct? I am writing several static utility classes with about 10-20 methods each. And each will have about four (4) overloaded methods so the static class definition (and the .cs source file) is getting pretty lengthy. Should I keep to the one-class, one-source-file convention, or is there a better way to organize my project in the Solution Explorer?

              realJSOPR Offline
              realJSOPR Offline
              realJSOP
              wrote on last edited by
              #6

              Don't worry about the length until the file gets to around 1000 lines of code. After that, you could try to find logical places to separate the code, and create partial classes.

              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

              1 Reply Last reply
              0
              • S ssclaire

                I believe it is tradition/convention to organize source files in a project so each class is contained in one .cs source file. Correct? I am writing several static utility classes with about 10-20 methods each. And each will have about four (4) overloaded methods so the static class definition (and the .cs source file) is getting pretty lengthy. Should I keep to the one-class, one-source-file convention, or is there a better way to organize my project in the Solution Explorer?

                A Offline
                A Offline
                Alan Balkany
                wrote on last edited by
                #7

                One class per source file is a good default convention, but a good programmer knows when to break the rules to improve clarity and simplicity. For example, it may be more maintainable to combine a few related classes that are only used together in the same file. Ask yourself: "What organizational structure would *I* prefer to work on when maintaining this system?"

                1 Reply Last reply
                0
                • S ssclaire

                  I believe it is tradition/convention to organize source files in a project so each class is contained in one .cs source file. Correct? I am writing several static utility classes with about 10-20 methods each. And each will have about four (4) overloaded methods so the static class definition (and the .cs source file) is getting pretty lengthy. Should I keep to the one-class, one-source-file convention, or is there a better way to organize my project in the Solution Explorer?

                  S Offline
                  S Offline
                  ssclaire
                  wrote on last edited by
                  #8

                  Thanks, folks. Good answers to my question.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups