Solved

C# code structure

Posted on 2003-10-25
3
228 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 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks :)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now