Click to See Complete Forum and Search --> : [RESOLVED] Classes and organization.


Pale
September 22nd, 2008, 05:17 PM
I have been learning to program in C# for about 2 months now, and i am failing to see how using more than one class (form1.cs) makes life easier.

Lets say you have an application that needs to send an email.

You could make another class called Email.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Emailer
{
public class Email
{
public void SendEmail( string to, string from )
{
//Sendemail("username", "from");
}
}
}

If you do it like above you then have to call it in your Form1.cs to use it.

private void btnCheck_Click( object sender, EventArgs e )
{
Email email = new Email ( );
email.SendEmail ( textox1.text, texbox2.text );
}

And from that i wonder if i should declare that instance of the email form ahead of time like:

private void Email email = new Email( );

That way i don't have to create a new instance of it for each event handler i need to use it in.



But wouldn't it be so much easier to do that in just Form1.cs?

If you do it in form1 you don't even need parameters
private void SendEmail( )
{
//Sendemail(txtbox1.text, textbox2.text);
}

Basically i need help understanding how to use multiple classes to my benefit. ( Probably in larger applications is where this would apply)

And someone to enlighten me on whether or not to create the instance of the class in the event handler or declare it in the form.

(I learn like Socrates proposed, through questioning and examining "Examples of other situations help" ;) )

Arjay
September 22nd, 2008, 10:50 PM
Sorry to say this, but it really depends on your needs.

For your simple example, consider if you needed to send mail from more than one place in the program (your UI has several forms and all can send email).

You could revise the class to be a static class with a static method:

static class Email
{
static void Send( string to, string from ) { }
}

This works well if the functionality is contained soley within the class method.

To use it...
private void btnCheck_Click( object sender, EventArgs e )
{
Email.Send( textox1.text, texbox2.text );
}

As far as determining whether to use a static class method (as I have done), create a class instance with local scope (as you have done) or to create an instance of the class as a form field depends on what the class has to do.

There are several criteria (a few of which are):
Does the class need to maintain state between method calls?
Is there signicant class initialization that needs to occur prior to any methods calls (i.e. is the class expensive to instantiate)?
Is the class shared between multiple objects (Forms, other classes, etc).

One thing that can help to determine which approach is this. Ask yourself if the class needs to stay around after its work method has been called? If the answer is no, then local instantiation or a static method may be the best approach.

Lastly one of the main goals we all strive for is reusability and to minimize the amount of duplicated code. I can't give you any hard and fast rules, but for a small amount of code (like 2 or 3 lines) used in only a couple of places, I might duplicate code somewhat in my application. However, if it's more lines of code and/or I'm using the code in several places, I'll typically put the functionality into a class. Now the next question is whether the class should reside in the application or become part of a class library (but we'll tackle that question later).

I generally prefer to break things out and most of the time the code that I write is very modular. For example, the following snippet is part of code that requests some files from an outside source, creates an file system event watcher. When all files have been transferred, the following event handler is called and some of the files are saved to the data base and other files are parsed and then saved to the db.

void OnTransferCompletedEvent( object sender, TransferCompletedEventArgs args )
{
FireTranslatorEvent( TranslatorEventTypes.ProcessingFiles );

SupportFileManager.Save( this );

CommandSetManager.Translate( this );

FireTranslatorEvent( TranslatorEventTypes.ProcessingComplete );
}

There are many ways to approach class design. I tend to go for the idea of general to specific such as the example above.

boudino
September 23rd, 2008, 04:01 AM
The key concept is separation of responsibility - single class for single purpose, don't mix more then one responsibility in single class. But where to cut and which responsibility (set of related operations) should go to which class is very hard to say. There is neither simple rule, nor "silver bullet". Right decomposition of the problem is the art of software development and this is why we are so good paid.

toraj58
September 23rd, 2008, 06:47 AM
When you are talking about big applications like Games for sure how to organize your code and classes is the most importatnt thing that can lead to failing of your application or succeeding it.

in big application when you are designing it you will be encountered with concepts like Inheritance; Polymorphism; abstraction; virtual methods and Interface and overriding also some more advance concepts like HAS A and IS A relationship between classes of your program.

if you go further you will be messed with algorithm and patterns that hundreds of them are out of them and i am sure soon or later you will be forced to use Singelton or Factory pattern in your apps that base of all of them is OOP.

In a nutshell i think no one can teach you how it would be better for you to organize your code and class although we have some standards out there and university courses but i think it is all about experince to be successful in this jurney.

for me even simplest programs should be Object Oriented and hopefully C# is the most object oriented language that have ever come to this world but in some condtions you may think that c++ is more object orinted like this fact that C++ supports mutiple inheritance but c# does not support but be happy becuase it is good news coz multiple inheritnace in C++ is error-prone and can complex the program but by the means of new comers to c# i mean Interface you can do the most job of its c++ equivalent with more efficiency.

as a hint i suggest you that start with most simple OOP issues like creating classes and instantiating them and Inheritance and then go forth soon you will find that is like the rules of universe that we living in it.

everything is object; think about levels of abstraction and find out what is the real Base Class in our universe that everything inherits from it!!!

Touraj Ebrahimi [toraj_e] [at] [yahoo] [dot] [com]

dannystommen
September 23rd, 2008, 10:01 AM
a very hard explanation for someone who's programming C# for 2 months.

Boudino's explantion is short en very clear in my opinion.

toraj58
September 23rd, 2008, 01:10 PM
most programmers are not good teachers! are you agree?

Pale
September 23rd, 2008, 03:21 PM
Thanks, i understand it.

And thanks to Arjay specifically for the good examples of code, i also figured out what the "static" modifier did from his post.

Thanks again. :)