InterviewSolution
| 1. |
Solve : Help with while statement in a vb 2010 program? |
|
Answer» Hello everyone, 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. |
|