[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

C# code structure

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

649 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