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
If you do it like above you then have to call it in your Form1.cs to use it.Code: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"); } } }
And from that i wonder if i should declare that instance of the email form ahead of time like:Code:private void btnCheck_Click( object sender, EventArgs e ) { Email email = new Email ( ); email.SendEmail ( textox1.text, texbox2.text ); }
That way i don't have to create a new instance of it for each event handler i need to use it in.Code:private void Email email = new Email( );
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
Basically i need help understanding how to use multiple classes to my benefit. ( Probably in larger applications is where this would apply)Code:private void SendEmail( ) { //Sendemail(txtbox1.text, textbox2.text); }
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")




)
Reply With Quote