Solved

C# code structure

Posted on 2003-10-25
3
234 Views
Last Modified: 2008-02-01
I'm just learning C# and am having difficulty figuring out where to put my code.  Do I put it all in Main() or create a seperate class for everything?  Do I create seperate CS files?  Create lots of different namespaces or just one?  If you know of an article that could help me with this it would be great :)  I'm sure it doesn't have to pertain only to C#. Probably any oop language would do.

Thanks :)
0
Comment
Question by:chilled2003
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 9

Accepted Solution

by:
tinchos earned 50 total points
ID: 9621717
This is what I'll tell you

You can place all in main(), create different classes, and create different .cs files for the classes. That is what you CAN do.

This is what I would do if I were you.


* As in all OO Languages, the idea is to make the classes needed (that the base of OOP). So I would think of the classes needed and implement them.


* You can place any number of classes in any .cs file. But if I were you I would try to place only 1 class in each .cs file (preferably the .cs file will have the name of the class inside), except for cases where you have auxiliary classes to the one placed in the .cs file.

For example

In this case, you have 2 classes, one (FullName) is auxiliary to the other one (Person), so in this case and as FullName will only be used in the person class, then I would put them both in the same .cs file (Person.cs).

class FullName
{
public string name;
public string surname;
}

class Person
{
     public string getName();
     public string getSurname();
     private FullName fullName;
}


* Refering to the namespaces.............

I would say that the namespace is something like the enviroment where the class belongs to..........

For instance........

Suppose you have 2 classes, one representing a graphic triangle (you can paint it in the screen) and the other one containing the conceptual model of a triangle

class Triangle
{
       void paint( int x, int y, Color color );     // paints a triangle in the given point with the given color
}

class Triangle
{
    int p1;
    int p2;
    int p3;
}

as both belong to different enviroments (enviroments is not the word I like most, but I couldnt think of any other better).

then I would do the following..

namespace Graphics
{

class Triangle
{
       void paint( int x, int y, Color color );     // paints a triangle in the given point with the given color
}

}


namespace Model
{

class Triangle
{
    int p1;
    int p2;
    int p3;
}

}


All this are guidelines, there are no rules for this........... Its a matter of style and experience.......... I believe that you'll find out that the best way to be comfortable with this subjects and to know what to do is to try...........
Experience will tell you what is best and what it isn't...........

Hope it helps

Tincho
0
 
LVL 10

Expert Comment

by:ptmcomp
ID: 9622288
Usually you make several classes where each of them has it's responsibility. It's not possible to explain the OO ideas here but Martin Fowler has good  explaining how to split your code in classes and methods. Look for books/articles about OO-Programming, XP (Xtreme Programming) and UML.
0
 

Author Comment

by:chilled2003
ID: 9623432
Thanks :)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual Studio hangs on running project 6 83
Unlocking a column in excel using C# 17 32
Problem to copy file 14 88
Include MS Database in Winforms Project using Intellilock 5 34
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question