?
Solved

C# code structure

Posted on 2003-10-25
3
Medium Priority
?
249 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
3 Comments
 
LVL 9

Accepted Solution

by:
tinchos earned 200 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Screencast - Getting to Know the Pipeline
Suggested Courses

839 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