separate/combine a code into a line?

xHBx

New member
Joined
Feb 8, 2019
Messages
3
Programming Experience
Beginner
Dear All expert

I could like to ask/request come help from you guy but I now sure how to ask in wording

basically I could like to know is there a way to separate/combine a code into a line?

Example
Button that trigger to sent a email the code will be like this

Visual Basic:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Try
        Dim Smtp_Server As New SmtpClient
        Dim e_mail As New MailMessage()
        Smtp_Server.UseDefaultCredentials = False
        Smtp_Server.Credentials = New Net.NetworkCredential("username@gmail.com", "password")
        Smtp_Server.Port = 587
        Smtp_Server.EnableSsl = True
        Smtp_Server.Host = "smtp.gmail.com"

        e_mail = New MailMessage()
        e_mail.From = New MailAddress(txtFrom.Text)
        e_mail.To.Add(txtTo.Text)
        e_mail.Subject = "Email Sending"
        e_mail.IsBodyHtml = False
        e_mail.Body = txtMessage.Text
        Smtp_Server.Send(e_mail)
        MsgBox("Mail Sent")

    Catch error_t As Exception
        MsgBox(error_t.ToString)
    End Try
End Sub

Possible to create another file like email.vb with all the email code inside and on the button code just call and run the code of the email.vb?

Visual Basic:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    'Code that able to trigger/run all the code in the email.vb

End Sub



I asking this is because my school project that I going to do the coding is getting longer and longer when I try to add new function in always corrupt the old working function and I need to redo again.

Hope someone able to guide me how should I separate and call the code from another .VB instead of writing all the code in the form1. Thanks

Regards,
HB
 
Last edited by a moderator:
If you're finding that adding new code is corrupting old code then you are most likely doing something wrong, so you should work out what and address that. It is possible that there is something wrong with your computer, you Windows installation or your VS installation, in which case you should address that, because it may lead to more serious issues. I would strongly recommend backing up your solution regularly in that case, which you should really be doing anyway.

As for the question, yes that is absolutely possible and probably advisable. Your classes should generally do just one thing each. It makes sense to have a class whose job it is to send email and then make use of that class anywhere that you ned to send email. The way you should generally approach this sort of refactoring is to first create a class with an appropriate name, e.g. EmailProvider, then add an appropriately named method, e.g. SendEmail, then copy your existing code into that method. At that point, the compiler will likely flag a few errors where you were using things that were specific to the original location of the form, e.g. txtFrom.Text. You should add a parameter to the method for each of those and then replace the flagged identifier with the parameter. Once you've addressed all the errors, you can then replace the original code with a call to the new method, passing the appropriate values as arguments.

Note that you can declare the method Shared if it makes sense to do so, in which case you can call the method on the class itself, without having to create an instance. If it makes more sense to do so, you can create an instance of the class each time and then call the method on that instance, so the method would not be declared Shared.
 
Hi thanks for your reply

Possible to show me a example/sample code to create a class and how do I call/Run/Trigger the class in a button?
 
Hi thanks for your reply

Possible to show me a example/sample code to create a class and how do I call/Run/Trigger the class in a button?
If you need to ask those questions then you need to get a better grasp of the basics of programming in VB. There's a link to a good tutorial in my signature below. You should follow that link and work your way through the tutorial.

As for the questions themselves, you already know how to do those things. If you have ever added a form to a project then you know how to create a class. If you haven't done that, you should learn and that tutorial can teach you. As for the rest, you're already doing that in the code you posted so you obviously know how to do it. You just don't know the terminology, which is exactly why you should work through a tutorial.
 
Back
Top