?
Solved

C# code structure

Posted on 2003-10-25
3
Medium Priority
?
238 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 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

752 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