You’ve all done driving test 1, right?

Since you’re right down to five marks for the first driving test now I’m expecting you’d all have done your first driving test by now. If not, then you probably need help.

So, we need to make a system with that can be used by customers to give their name and email address, and a comment, and then have it emailed to you with all that information. It’s really fairly simple, especially since in the book you’ve been given the following code.

Dim A
Dim B
Dim C
A = txtEMail.Text
B = txtName.Text
C = txtComment.Text
Dim myMessage As New System.Net.Mail.MailMessage(A, "Your E-mail", B, C)
Dim Server As New System.Net.Mail.SmtpClient("mail.cse.dmu.ac.uk")
Server.Send(myMessage)
txtName.Text = ""
txtEMail.Text = ""
txtComment.Text = ""

One snag here is that that’s awful code. If you ever sold that to a client they probably wouldn’t be too happy with it. It’s pretty damn disgraceful. The variable declarations have been given awful names, and they haven’t even been given data types!

First, quickly mock up a web page. Making it look like an actual company’s website isn’t really that important; your tutors more bothered about if you can actually do the code at the moment. In your brief (in the book) underline what information you need to be input by the user, so you know exactly what needs to be put on the page. As it is, all you need is a box for their name, email address, and a final one for their comment.

Design doesn't matter so much

Design doesn't matter so much

Remember to give the controls good IDs! In fact, you’re given the IDs to use in the code sample.

You want the code to be run when the user clicks on the button, so double click on that submit button to open the code for that event.

The next thing you should definitely deal with is fixing the variables that have been declared there. Give them better names! A, B, and C really aren’t cool. You’ve no idea what they do, and you’ll just confuse yourself by the end of the code. Not every project you make is going to be less than a page of code long, so you won’t be able to just look at the top of the screen to find where you set a variable. Always use good variable names. When you change the variable names, make sure you change them in all the places they’re mentioned later on.

Also, you’ll need to add the As Datatype bit to the Dims too. Think about what data your user will be putting into them.

Now ladies and gentlemen, listen to what I say next very damn carefully. Go through the code and add comments. Honest to what ever god you follow, if you don’t you will fail. You need to comment to a huge degree, even more than you would in actual industary. Add something like this.

‘This variable will be used later to store the customer’s name

That’s all. I’m guessing most of you will be confused by how to comment your MyMessage and Server, so let me just explain them now. MyMessage is an object which you’ve created to hold an email. You’ve given it some data, like the to address, from address, subject, and body. The server object is just an object which holds data about your server. Simple really.

It’s easy to forget which order the parameters of the email object go in, and don’t worry, even professionals will. Programming isn’t about memorising thousands of commands. Every programmer will quickly become best friends with the documentation of their language. To get to the documentation, just press F1 when your cursor is on the thing that’s confusing you. For the record the parameters are:

From, To, Subject, Body

When every you get confused by what you’re doing, you should say outloud your aim again. You want to send an email to yourself, from a customer, with their comment.

And after that, you’re pretty much finished! Here’s an example of the code you should have ended up with. Ffs, don’t copy and paste this to use. If you don’t understand it, your lecturer will know straight away it’s not yours, and my tutor has already seen these comments in my code.

'Declare a space in memory to hold the customer's name
Dim UsersName As String
'Used to hold the users Email address
Dim UsersEmail As String
'Used to hold the users comment
Dim UsersComment As String
'Grab all the inputs and put them into the right variables
UsersEmail = txtEMail.Text
UsersName = txtName.Text
UsersComment = txtComment.Text
'Create the object to store the email we want to send, with all the right parameters for the to,
'from, subject and body
Dim myMessage As New System.Net.Mail.MailMessage(UsersEmail, "shamess@gmail.com", UsersName, UsersComment)
'Create an object to store server details
Dim Server As New System.Net.Mail.SmtpClient("mail.cse.dmu.ac.uk")
'Connect to the server and send the email
Server.Send(myMessage)
'Now we can clear all the text boxes
txtName.Text = ""
txtEMail.Text = ""
txtComment.Text = ""

Now you’re ready to take your driving test, even though it’s kinda late. Start thinking about your second driving test now.

You still can’t relax though. Your tutor is going to ask you three questions about your code, and/or maybe even ask you to add an extra feature. Here are some possible things they could ask:

  • Add a cancel button, to clear the form but not submit it
  • Make it so the body of the email also has the customer’s name in it. (Until I write a tutorial, here’s a Wikipedia page on concatenation.
  • Make it so the email is sent to a different address
  • Add a thank you message after the email has been sent

Comments are closed.