I have two text boxes, submit button. and a dropdown list with the items(even, odd, and all). I want to calculate the sum of either all "Even", "Odd" or "All" numbers according the selection. In the code below, I have the "all" section working correctly but I'm trying to figure out a way to get the Even and Odd to work.

Some one suggested me to develope a good formula for determining whether a number is even or odd, then calling that from within the program as needed. I've tried that below with the IsEvenNumber function but got stuck. Any suggestions? Thanks

``````Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
If Me.ddlChoices.SelectedValue = "Even" Then
Even_Numbers()
End If
If Me.ddlChoices.SelectedValue = "Odd" Then
Odd_Numbers()
End If
If Me.ddlChoices.SelectedValue = "All" Then
All_Numbers()
End If
End Sub

'suggestion from a friend
Private Function IsEvenNumber(ByVal input As Integer) As Boolean
IsEvenNumber = (input Mod 2 = 0)

If input Mod 2 = 0 Then
Return True
Else
Return False
End If

End Function

'sum of all
Public Sub All_Numbers()
Dim var As Integer
Dim intcount As Integer
Dim intcount2 As Integer

intcount = CInt(txtOption1.Text)
intcount2 = CInt(txtOption2.Text)
var = 0

Do While intcount <= intcount2

var = var + intcount
intcount = intcount + 1

Loop

Me.lblShow.Text = "The answer is:" & var
End Sub

'sum of odd
Public Sub Odd_Numbers()
Dim var As Integer
Dim intcount As Integer
Dim intcount2 As Integer

intcount = CInt(txtOption1.Text)
intcount2 = CInt(txtOption2.Text)
var = 0

Do While intcount <= intcount2

var = var + intcount
intcount = intcount + 2

Loop

Me.lblShow.Text = "The answer is:" & var
End Sub

'sum of even
Public Sub Even_Numbers()
Dim var As Integer
Dim intcount As Integer
Dim intcount2 As Integer

intcount = CInt(txtOption1.Text)
intcount2 = CInt(txtOption2.Text)
var = 1

Do While intcount <= intcount2

var = var + intcount
intcount = intcount + 2

Loop

Me.lblShow.Text = "The answer is:" & var
End Sub``````

First thing is var is a reserved word. Don't use it as a variable.
Second, you went to the trouble making `IsEvenNumber()` but you don't call it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.