locked
multiple CS Files (CS newbee) RRS feed

  • Question

  •  

    I am a C++ programmer and I am working on my first C# program. Of course, my mind is still thinking C++.  I  have a very BASIC question.  (Please forgive my ignorance.)

     

    Form1.cs has gotten to Long.  I don't like long files.  In C++/CLI I simply create another cpp, prototype my function in Form1.h, include Form1.h in the cpp file, and define the function with access to anything in Form1.cpp.   CS does not have a .h.  It seems like all functions are expected to be contained in ONE file. Surely not!  I'm sure It's just  ignorance on my part.

     

    So how do I break up a long file into multiple cs files?

    Friday, November 16, 2007 7:26 PM

Answers

  • If I understand your question correctly, you want to split a single class into multiple physical files. If this is what you intend to do, then you can simply define your class with the partial keyword like:

     

    public partial class Form1

    {

         ........

    }

     

    and write code in it. When you want to span this code to another physical file then add a .cs file from your designer as specified by BigTuna99 and then write the following again and write whatever code you want to:

     

    public partial class Form1

    {

         ........

    }

     

    When the compiler compiles your project, it will automatically merge the code in both the files and things will work the same way as it would have if you had one file.

     

    Please note that this feature exists only from .Net 2.0 onwards. So if you are using a version below .Net 2.0 please ignore everything above.

     

    If your intent is not to split a class into 2 or more physical files and just want to define class1 in one physical file and class2 in another physical file, then you can simply create two physical files, one for class1 and another for class2 and make sure that the two classes are in the same namespace.

     

    cheers

    Sharad

     

    Friday, November 16, 2007 7:49 PM

All replies

  • You could create a Class by right clicking on your project and choosing add->Class then put whatever code you want in that class.  Then you can access that class directly in your form.

     

     


    If this answers your question, please mark the question as answered.
    Friday, November 16, 2007 7:33 PM
  • If I understand your question correctly, you want to split a single class into multiple physical files. If this is what you intend to do, then you can simply define your class with the partial keyword like:

     

    public partial class Form1

    {

         ........

    }

     

    and write code in it. When you want to span this code to another physical file then add a .cs file from your designer as specified by BigTuna99 and then write the following again and write whatever code you want to:

     

    public partial class Form1

    {

         ........

    }

     

    When the compiler compiles your project, it will automatically merge the code in both the files and things will work the same way as it would have if you had one file.

     

    Please note that this feature exists only from .Net 2.0 onwards. So if you are using a version below .Net 2.0 please ignore everything above.

     

    If your intent is not to split a class into 2 or more physical files and just want to define class1 in one physical file and class2 in another physical file, then you can simply create two physical files, one for class1 and another for class2 and make sure that the two classes are in the same namespace.

     

    cheers

    Sharad

     

    Friday, November 16, 2007 7:49 PM
  • I have to disagree with Sharad Nair's suggestion: partial classes should only be used to separate machine-generated code from human-generated code.  If your class is too big to fit in one file, then it's probably too big period.  #region / #endregion can help within a single file, though.

    Creating additional classes (as BigTuna99 suggests) is recommended.  Each class should logically group related components.  If you start to get a lot of classes, consider breaking them up with namespaces (Visual Studio autmatically treats folders in a project as a namespace).

    In C# (and VB), there is no "header" concept.  Every class/struct/etc is visible everywhere, within an assembly.  (Outside of assemblies it depends on the accessor of the type, public vs. internal, basically.)


    -Ryan / Kardax
    Friday, November 16, 2007 9:28 PM
  • I have to disagree with you Ryan. While file size/line count should be kept down, and ideally you can compartmentalize a larger object into a collection of smaller objects. However there are times when this doesn't make sense or just isn't possible. Does that mean that you're stuck writing "bad" code by having a large class in a single file? Partial classes can address this allowing for yet another style of grouping within the same class conceptually. This does come with that you should always when moving to partial classes to reduce size check to see first if there is anything you can consider and implement as a class by itself.

    Just to be accurate
     Ryan Lamansky / Kardax wrote:

    In C# (and VB), there is no "header" concept.  Every class/struct/etc is visible everywhere, within an assembly.  (Outside of assemblies it depends on the accessor of the type, public vs. internal, basically.)

    -Ryan / Kardax

    is incorrect, even inside an assembly there are ways of having class/struct/etc not be visible everywhere; private nested classes or private structs inside a class bear to mind. We have public, internal, protected, and private all for a reason.
    Friday, November 16, 2007 11:37 PM
  •  

    I like this answer.  And will do it. I am using Framework 2.0.

     

    As for the compartmentalization of code into class.  I undersand that stuff (newbee in C#, not C++/CLI) though my classic C and Fortran background taught me very bad habits of writting long functions.

     

    It is not that my class is unweldingly long.  It's just that I like to keep order with more files than some might.  Again, my classic C and emacs, Pre IDE, background  -- when each function was a separate file, and directories held major divisions. I'm an older man and old habits die hard! 

     

    Thanks all for the responses!

     

     

    Saturday, November 17, 2007 2:08 AM
  •  Ryan Lamansky / Kardax wrote:
    I have to disagree with Sharad Nair's suggestion: partial classes should only be used to separate machine-generated code from human-generated code.  If your class is too big to fit in one file, then it's probably too big period.  #region / #endregion can help within a single file, though.

    Creating additional classes (as BigTuna99 suggests) is recommended.  Each class should logically group related components.  If you start to get a lot of classes, consider breaking them up with namespaces (Visual Studio autmatically treats folders in a project as a namespace).

    In C# (and VB), there is no "header" concept.  Every class/struct/etc is visible everywhere, within an assembly.  (Outside of assemblies it depends on the accessor of the type, public vs. internal, basically.)


    -Ryan / Kardax
    I'm curious why'd you'd say that?  Partial classes support two or more files.  One use for them is to separate machine-generated code from human-generated/modified code; but that's not the only use for them.  I would agree and not recommend using partial classes where a re-design is needed.  But, there's many mitigating circumstances that would affect someones ability to redesign a class.  (like it's a legacy class that is currently being used by external applications and a re-design would require coordination of those external groups and/or you still have to maintain the current version and splitting the class across multiple files would make maintaining it much easier).

    Saturday, November 17, 2007 4:41 PM
  •  Peter Ritchie wrote:

    I'm curious why'd you'd say that?  Partial classes support two or more files.  One use for them is to separate machine-generated code from human-generated/modified code; but that's not the only use for them.  I would agree and not recommend using partial classes where a re-design is needed.  But, there's many mitigating circumstances that would affect someones ability to redesign a class.  (like it's a legacy class that is currently being used by external applications and a re-design would require coordination of those external groups and/or you still have to maintain the current version and splitting the class across multiple files would make maintaining it much easier).



    Certainly there are cases where you have to do undesirable things just cuz there's no other way (I've used "goto" on rare occasions, for example)... but in general I strictly adhere to one-type-per-file in my projects.

    The moment you throw a "partial" onto a class, it can be spread across (theoretically) hundreds of files.  I'm not comfortable with things being so open-ended Smile

    -Ryan / Kardax
    Saturday, November 17, 2007 7:10 PM
  • Ryan,

    Isn't that what folder structure in Solution Explorer is for?  I create partial classes all of the time, but I place all of the code files into a common folder within SE.  Your comment suggests that you all too easily loose track of your files.

     

    I,too, am from the old school and love the concept of splitting up the code into easily managed and debugged files.  In fact, I go to the extreme of having a sub-folder within that "class" folder for code that needs work.  When that file is performing as I want, I drag and drop into the parent folder.  This approach works great when I use the class designer, which just loves to create methods that contain exceptions reminding that the method has not yet been implemented.  I even go to the extreme of moving those unfinished members from one partial class file to another when the are completed.  By using this sub-folder approach, I can see at a glance what needs attention and what does not.

     

    As noted, splitting up the code was not possible with some of the older language designs.  Memory and storage space simply cost too much.  Everyone has their own style of writing code.  What works for you, is what is best for you.  What works for others is what best for them.  There never is any single right way to do anything.  Isn't that what engineering is all about?  Finding better designs, and methods.

     

    Rudedog

    "Stuck in procedural world."

     

    Sunday, November 18, 2007 4:16 PM
  •  IsshouFuuraibou wrote:
    I have to disagree with you Ryan. While file size/line count should be kept down, and ideally you can compartmentalize a larger object into a collection of smaller objects. However there are times when this doesn't make sense or just isn't possible. Does that mean that you're stuck writing "bad" code by having a large class in a single file? Partial classes can address this allowing for yet another style of grouping within the same class conceptually. This does come with that you should always when moving to partial classes to reduce size check to see first if there is anything you can consider and implement as a class by itself.

     

    I've tried both approaches and can say from my experience that breaking up a large unwieldy class/file into smaller classes is two orders of magnitude more difficult and time consuming than creating a partial class or two. If you were smart enough to create separate class for a piece of code from the start that is fine of course. You have to have a foresight for it.

    Sunday, November 18, 2007 6:49 PM
  • I am new to C# too and I like it alot but I too come from a C++ background and even though I am a novice. I make no claim on being an expert but is partial class the same thing as overloaded functions. Again any clarification would be nice. Also, I think you are all not getting what he is asking, In C++ you create your classes and namespaces in a header file, then you create a .cpp file to define your functions. I too am confused at the setup of files when it comes to C#. Basically what is the equivalent to header files in C# or do you just make a new file per namespace and class. If I am not saying this right forgive me I am new.

     Thanks in advance

    William

    Thursday, May 11, 2017 11:24 PM