1.

Solve : Help with while statement in a vb 2010 program?

Answer»

Hello everyone,

I'm WORKING on a program that will allow me to send a single email multiple times to the same recipient. To do this I'm using a while statement, but it doesn't seem to work. Here's what I have.

Code: [Select]Dim i As New Integer

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
i = 0
dim e as new integer
e = Textbox16.text
Try
While Not i = e
SendMail()
i = i + 1
End While
MsgBox("All Mail Sent")
Catch ex As Exception
e = 0
i = e
MsgBox(ex)
End Try
End Sub
The SendMail() sub is here:

Code: [Select]Private Sub SendMail()
Try
Dim SmtpServer As New SmtpClient
Dim mail As New MailMessage
SmtpServer.EnableSsl = True
SmtpServer.Credentials = New Net.NetworkCredential(TextBox1.Text, TextBox2.Text)
SmtpServer.Port = TextBox3.Text
SmtpServer.Host = TextBox4.Text
mail = New MailMessage
mail.From = New MailAddress(TextBox3.Text)
mail.To.Add(TextBox10.Text)
mail.Subject = TextBox9.Text
mail.Body = TextBox8.Text
SmtpServer.Send(mail)
Catch ex As Exception
End Try
End Sub
When will the statement i = e be true?
Quote from: Salmon Trout on October 22, 2012, 12:50:59 PM

When will the statement i = e be true?

In the while statement it has:
Code: [Select]i = i + 1This should add 1 to i until i = eAnd is that happening?
Quote from: Salmon Trout on October 24, 2012, 07:17:40 AM
And is that happening?

No, it completely skips the while statement and goes straight to the messageboxAnd what is i?

Even if it is a form-level variable, the code won't compile. e is declared as a local variable but it already exists as a PARAMETER to the event method.

Ignoring those two problems, The code seems to work mostly as intended. Though if the input value was 50 it actually iterates 51 times.

I also wonder why you would use a While Loop to begin with. For...Next seems to do what you are after and more concisely:

Code: [Select] Private Sub Button1_Click(ByVal sender As System.Object, ByVal ev As System.EventArgs) Handles Button1.Click

Dim e As Integer
If Not Integer.TryParse(TextBox1.Text, e) Then
MessageBox.Show("Please Enter a Number")
End If
e = TextBox1.Text
For i = 1 To e
SendMail()
Next i
MessageBox.Show("All Mail Sent")

End Sub
I changed the parameter name to ev, and I used the equally indescriptive name for the textbox, TextBox1. Just has a better ring than TextBox16. I was going to go overboard and call it "txtSendAmount" but then somebody might be able to make sense of what it is for, and we wouldn't want that. I also used Integer.TryParse() simply because I never liked the IDEA of implicit type coercion at least as implemented in Visual Basic. Too unreliable. And more importantly, it can throw an exception which in the original code would have been unhandled. Best to make Exceptions EXCEPTIONAL and avoid causing them to begin with.

Another point is that The Try...Catch block in the Button event routine is moot. The only thing that could throw an exception within the try...catch would be the call to SendMail(), but that method already silently catches all exceptions anyway. More importantly, "Pokemon" exception handling ("Gotta catch em all") is not generally a good idea.




Discussion

No Comment Found