Solved

# vb.net code help

Posted on 2011-04-28

I am working on a class project, it's an on-line class so instructor help is hard to come by. I'm not looking for the answer, just a nudge in the right direction.

The project is to create a Prime Number application using For...Next statements and the mod operator. We are supposed to be able to enter any 2 numbers and the program should display all primes between. Below is my code so far, do you guys see anything obvious? The error codes work well already. I've got about 8 weeks of experience, so don't laugh too hard!

Private Sub CalcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcButton.Click

Dim upper As Integer

Dim lower As Integer

Dim squareRoot As Integer

'Declare variables

lower = Val(LowerTextBox.Text)

upper = Val(UpperTextBox.Text)

squareRoot = Math.Sqrt(UpperTextBox.Text)

' Display error messages for invalid bounds

If lower < 2 OrElse upper < 2 Then

MessageBox.Show("Bounds must be greater than 1", _

"Invalid Bounds", MessageBoxButtons.OK, _

MessageBoxIcon.Error)

Else

If lower > Val(upper) Then

MessageBox.Show("Upper bound cannot be less than lower bound", _

"Invalid Bounds", MessageBoxButtons.OK, _

MessageBoxIcon.Error)

End If

End If

For counter As Integer = lower To upper

For counter2 As Integer = lower To squareRoot

'Call Prime function

If Prime(counter, counter2) = True Then ResultsTextBox.AppendText(counter)

Next

ResultsTextBox.AppendText(vbCrLf)

Next

End Sub

'Prime function

Function Prime(ByVal counter As Integer, ByVal counter2 As Integer) As Boolean

If counter2 Mod counter = 0 Then

Return True

Else : Return False

End If

End Function