472,095 Members | 2,514 Online

# How to distinguish odd / even numbers and sumthem

I want to write a program that will accept a number in a textbox for
example 23578 and then in a label will display the sum of the odd and
even number like this...

the textbox containsthe number 23578
the label would say:
Sumof odd number is: 15
Sum of even number is: 10

any ideas?
thanks

Jan 24 '07 #1
17 4445
This sounds like a homework problem? If so, you should really work this out
on your own and not post here. It's really hard to imagine a real world
application wanting to do this, and a real world programmer not knowing how
to accomplish this...

"Ron" <pt*****@yahoo.comwrote in message
>I want to write a program that will accept a number in a textbox for
example 23578 and then in a label will display the sum of the odd and
even number like this...

the textbox containsthe number 23578
the label would say:
Sumof odd number is: 15
Sum of even number is: 10

any ideas?
thanks

Jan 24 '07 #2
Plus think "in English" how you would explain step by step to someone how to
do this. Then tell the same to the computer. For example the first step
would be likely to extract each digit for processing (until the total length
is reached), once you have a digit what would you do and so on...

Welcome to programming...

"Marina Levit [MVP]" <ma*****@comcast.neta écrit dans le message de news:
xZ******************************@comcast.com...
This sounds like a homework problem? If so, you should really work this
out on your own and not post here. It's really hard to imagine a real
world application wanting to do this, and a real world programmer not
knowing how to accomplish this...

"Ron" <pt*****@yahoo.comwrote in message
>>I want to write a program that will accept a number in a textbox for
example 23578 and then in a label will display the sum of the odd and
even number like this...

the textbox containsthe number 23578
the label would say:
Sumof odd number is: 15
Sum of even number is: 10

any ideas?
thanks

Jan 24 '07 #3
"Ron" <pt*****@yahoo.comschrieb:
>I want to write a program that will accept a number in a textbox for
example 23578 and then in a label will display the sum of the odd and
even number like this...

the textbox containsthe number 23578
the label would say:
Sumof odd number is: 15
Sum of even number is: 10
Check out the 'Mid' function, the 'For' loop, and the 'Mod' statement.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Jan 24 '07 #4
Well that will get him a C- :-)

"Lost" <lo*******@mailinator.comwrote in message
"Ron" <pt*****@yahoo.comwrote:
>>I want to write a program that will accept a number in a textbox for
example 23578 and then in a label will display the sum of the odd and
even number like this...

the textbox containsthe number 23578
the label would say:
Sumof odd number is: 15
Sum of even number is: 10

any ideas?
thanks

Dim strNumber As String
Dim strTemp As String
Dim intIndex As Integer
Dim intTotEvens As Integer
Dim intTotOdds As Integer

intTotOdds = 0
intTotEvens = 0
strNumber = Trim(TextBox1.Text) ' contains input, eg 23578
For intIndex = 1 To Len(strNumber)
strTemp = Mid(strNumber, intIndex, 1)
If InStr("02468", strTemp) 0 Then
intTotEvens = intTotEvens + Val(strTemp)
Else
intTotOdds = intTotOdds + Val(strTemp)
End If
Next intIndex
Label1.Text = "Sum of odd numbers is: " + Str(intTotOdds)
Label2.Text = "Sum of even numbers is: " + Str(intTotEvens)

--
Modo vincis, modo peris

Jan 24 '07 #5

In my day, we had the smarts to cheat on our homework assignments in a place
that wasn't 100% public and archived for all history with a search tool
that's accessed daily by billions of people.

Young people these days. Sheesh.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, MVP C#
http://www.coversant.net/blogs/cmullins

"Ron" <pt*****@yahoo.comwrote in message
>I want to write a program that will accept a number in a textbox for
example 23578 and then in a label will display the sum of the odd and
even number like this...

the textbox containsthe number 23578
the label would say:
Sumof odd number is: 15
Sum of even number is: 10

any ideas?
thanks

Jan 25 '07 #6
Dim intTotEvens As Integer
Dim intTotOdds As Integer

Dim strNumber As String = TextBox1.Text.Trim() ' contains input, eg 23578

For intIndex As Integer = 0 To strNumber.Length - 1
Dim strTemp As String = strNumber.SubString(intIndex, 1)
If ("02468").IndexOf(strTemp) >= 0 Then
intTotEvens += Integer.Parse(strTemp)
Else
intTotOdds += Integer.Parse(strTemp)
End If
Next

Label1.Text = "Sum of odd numbers is: " & intTotOdds.ToString()

Label2.Text = "Sum of even numbers is: " & intTotEvens.ToString()

Does that at least get me a C+? :)

Dim _evens As Integer
Dim _odds As Integer

For _i As Integer = 0 To TextBox1.Text.Length - 1
Dim _v As Integer = Integer.Parse(TextBox1.Text.SubString(_i, 1))
If _v Mod 2 = 0 Then
_evens += _v
Else
_odds += _v
End If
Next

Label1.Text = "Sum of odd numbers is: " & _odds.ToString()

Label2.Text = "Sum of even numbers is: " & _evens.ToString()
"Tom Leylan" <tl*****@nospam.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Well that will get him a C- :-)

"Lost" <lo*******@mailinator.comwrote in message
>"Ron" <pt*****@yahoo.comwrote:
>>>I want to write a program that will accept a number in a textbox for
example 23578 and then in a label will display the sum of the odd and
even number like this...

the textbox containsthe number 23578
the label would say:
Sumof odd number is: 15
Sum of even number is: 10

any ideas?
thanks

Dim strNumber As String
Dim strTemp As String
Dim intIndex As Integer
Dim intTotEvens As Integer
Dim intTotOdds As Integer

intTotOdds = 0
intTotEvens = 0
strNumber = Trim(TextBox1.Text) ' contains input, eg 23578
For intIndex = 1 To Len(strNumber)
strTemp = Mid(strNumber, intIndex, 1)
If InStr("02468", strTemp) 0 Then
intTotEvens = intTotEvens + Val(strTemp)
Else
intTotOdds = intTotOdds + Val(strTemp)
End If
Next intIndex
Label1.Text = "Sum of odd numbers is: " + Str(intTotOdds)
Label2.Text = "Sum of even numbers is: " + Str(intTotEvens)

--
Modo vincis, modo peris

Jan 25 '07 #7
I guess I sort of stuck my foot in my mouth on that one... now I have to
post something :-) Glad "Lost" took it with humor. What I love about these
homework assignments is that they create small topics from which solutions
can be discussed. Questions like "how do I write a video game" isn't quite
going to be solved by each of us submitting our collision detection routine.

I'd suggest a couple of things. First the routine should be written
independently of the source of input so I wouldn't grab the data out of the
textbox but would require that to be passed to the routine. I opted to
display the values directly into the labels though. The reason is the extra
code needed to initialize them (outside of the routine) and to pass them by
reference just obfuscates things (since (I doubt) this is going to be part
of a reusable library.)

The other was testing the characters as "strings" is problematic. The
solutions tended to insist if a character wasn't "0,2,4,6 or 8" then it must
have been "1,3,5,7 or 9" and I don't know the user didn't key in
"ab383994*0".

I decided to add some functionality that I might be able to use in the
future by creating IsEven() and IsOdd() functions. While not necessary I'd
hate to write ((value MOD 2) = 0 ) the next time I needed to know. Uh,
let's see I opted for the Int32 declaration (not trying to start a fight but
I prefer it too). And I used TryParse so I can determine when non-digits
were entered and I can ignore them.

So here is mine:

' upon some click event or something
ValueCounter(txtInput.Text.Trim())
Private Sub Test7(ByVal sInput As String)

Dim nEvens As Int32 = 0
Dim nOdds As Int32 = 0

Dim bIsDigit As Boolean
Dim nValue As Int32 = 0

For i As Integer = 0 To (sInput.Length - 1)
bIsDigit = Int32.TryParse(sInput.Substring(i, 1), nValue)
If bIsDigit Then
If IsEven(nValue) Then
nEvens += nValue
ElseIf IsOdd(nValue) Then
nOdds += nValue
End If
End If
Next

lblEven.Text = "Sum of even numbers is: " + nEvens.ToString()
lblOdd.Text = "Sum of odd numbers is: " + nOdds.ToString()

End Sub

Public Function IsEven(ByVal num As Int32) As Boolean
Return ((num Mod 2) = 0)
End Function

Public Function IsOdd(ByVal num As Int32) As Boolean
Return (Not IsEven(num))
End Function

"Stephany Young" <noone@localhostwrote in message
news:us*************@TK2MSFTNGP06.phx.gbl...
Dim intTotEvens As Integer
Dim intTotOdds As Integer

Dim strNumber As String = TextBox1.Text.Trim() ' contains input, eg 23578

For intIndex As Integer = 0 To strNumber.Length - 1
Dim strTemp As String = strNumber.SubString(intIndex, 1)
If ("02468").IndexOf(strTemp) >= 0 Then
intTotEvens += Integer.Parse(strTemp)
Else
intTotOdds += Integer.Parse(strTemp)
End If
Next

Label1.Text = "Sum of odd numbers is: " & intTotOdds.ToString()

Label2.Text = "Sum of even numbers is: " & intTotEvens.ToString()

Does that at least get me a C+? :)

Dim _evens As Integer
Dim _odds As Integer

For _i As Integer = 0 To TextBox1.Text.Length - 1
Dim _v As Integer = Integer.Parse(TextBox1.Text.SubString(_i, 1))
If _v Mod 2 = 0 Then
_evens += _v
Else
_odds += _v
End If
Next

Label1.Text = "Sum of odd numbers is: " & _odds.ToString()

Label2.Text = "Sum of even numbers is: " & _evens.ToString()
"Tom Leylan" <tl*****@nospam.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>Well that will get him a C- :-)

"Lost" <lo*******@mailinator.comwrote in message
>>"Ron" <pt*****@yahoo.comwrote:

I want to write a program that will accept a number in a textbox for
example 23578 and then in a label will display the sum of the odd and
even number like this...

the textbox containsthe number 23578
the label would say:
Sumof odd number is: 15
Sum of even number is: 10

any ideas?
thanks

Dim strNumber As String
Dim strTemp As String
Dim intIndex As Integer
Dim intTotEvens As Integer
Dim intTotOdds As Integer

intTotOdds = 0
intTotEvens = 0
strNumber = Trim(TextBox1.Text) ' contains input, eg 23578
For intIndex = 1 To Len(strNumber)
strTemp = Mid(strNumber, intIndex, 1)
If InStr("02468", strTemp) 0 Then
intTotEvens = intTotEvens + Val(strTemp)
Else
intTotOdds = intTotOdds + Val(strTemp)
End If
Next intIndex
Label1.Text = "Sum of odd numbers is: " + Str(intTotOdds)
Label2.Text = "Sum of even numbers is: " + Str(intTotEvens)

--
Modo vincis, modo peris

Jan 25 '07 #8
You're really having fun today Tom! ;)

Shouldn't it be:

' upon some click event or something
Test7(txtInput.Text.Trim())

But now we're really cooking with gas:

Dim oae As New OddsAndEvens

If oae.TotalOddsAndEvens(txtInput.Text.Trim()) Then
lblEven.Text = "Sum of even numbers is: " & oae.TotalOfEvens.ToString()
lblOdd.Text = "Sum of odd numbers is: " & oae.TotalOfOdds.ToString()
Else
MessageBox.Show("Supplied value is not a valid value")
End If

Public Class OddsAndEvens

Private m_odds As Int32
Private m_evens As Int32

Public ReadOnly Property TotalOfOdds() As Int32
Get
Return m_odds
End Get
End Property

Public ReadOnly Property TotalOfEvens() As Int32
Get
Return m_evens
End Get
End Property

Public Function TotalOddsAndEvens(value As String) As Boolean

m_odds = 0
m_evens = 0

Dim nValue As Int32 = 0

For i As Integer = 0 To value.Length - 1
If Int32.TryParse(sInput.Substring(i, 1), nValue)
If IsEven(nValue) Then
nEvens += nValue
ElseIf IsOdd(nValue) Then
nOdds += nValue
End If
Else
Return False
End If
Next

Return False

End Sub

Public Shared Function IsEven(ByVal num As Int32) As Boolean
Return (num Mod 2 = 0)
End Function

Public Shared Function IsOdd(ByVal num As Int32) As Boolean
Return (Not IsEven(num))
End Function

End Class
"Tom Leylan" <tl*****@nospam.netwrote in message
news:er**************@TK2MSFTNGP05.phx.gbl...
>I guess I sort of stuck my foot in my mouth on that one... now I have to
post something :-) Glad "Lost" took it with humor. What I love about
these homework assignments is that they create small topics from which
solutions can be discussed. Questions like "how do I write a video game"
isn't quite going to be solved by each of us submitting our collision
detection routine.

I'd suggest a couple of things. First the routine should be written
independently of the source of input so I wouldn't grab the data out of
the textbox but would require that to be passed to the routine. I opted
to display the values directly into the labels though. The reason is the
extra code needed to initialize them (outside of the routine) and to pass
them by reference just obfuscates things (since (I doubt) this is going to
be part of a reusable library.)

The other was testing the characters as "strings" is problematic. The
solutions tended to insist if a character wasn't "0,2,4,6 or 8" then it
must have been "1,3,5,7 or 9" and I don't know the user didn't key in
"ab383994*0".

I decided to add some functionality that I might be able to use in the
future by creating IsEven() and IsOdd() functions. While not necessary
I'd hate to write ((value MOD 2) = 0 ) the next time I needed to know.
Uh, let's see I opted for the Int32 declaration (not trying to start a
fight but I prefer it too). And I used TryParse so I can determine when
non-digits were entered and I can ignore them.

So here is mine:

' upon some click event or something
ValueCounter(txtInput.Text.Trim())
Private Sub Test7(ByVal sInput As String)

Dim nEvens As Int32 = 0
Dim nOdds As Int32 = 0

Dim bIsDigit As Boolean
Dim nValue As Int32 = 0

For i As Integer = 0 To (sInput.Length - 1)
bIsDigit = Int32.TryParse(sInput.Substring(i, 1), nValue)
If bIsDigit Then
If IsEven(nValue) Then
nEvens += nValue
ElseIf IsOdd(nValue) Then
nOdds += nValue
End If
End If
Next

lblEven.Text = "Sum of even numbers is: " + nEvens.ToString()
lblOdd.Text = "Sum of odd numbers is: " + nOdds.ToString()

End Sub

Public Function IsEven(ByVal num As Int32) As Boolean
Return ((num Mod 2) = 0)
End Function

Public Function IsOdd(ByVal num As Int32) As Boolean
Return (Not IsEven(num))
End Function

"Stephany Young" <noone@localhostwrote in message
news:us*************@TK2MSFTNGP06.phx.gbl...
> Dim intTotEvens As Integer
Dim intTotOdds As Integer

Dim strNumber As String = TextBox1.Text.Trim() ' contains input, eg
23578

For intIndex As Integer = 0 To strNumber.Length - 1
Dim strTemp As String = strNumber.SubString(intIndex, 1)
If ("02468").IndexOf(strTemp) >= 0 Then
intTotEvens += Integer.Parse(strTemp)
Else
intTotOdds += Integer.Parse(strTemp)
End If
Next

Label1.Text = "Sum of odd numbers is: " & intTotOdds.ToString()

Label2.Text = "Sum of even numbers is: " & intTotEvens.ToString()

Does that at least get me a C+? :)

Dim _evens As Integer
Dim _odds As Integer

For _i As Integer = 0 To TextBox1.Text.Length - 1
Dim _v As Integer = Integer.Parse(TextBox1.Text.SubString(_i, 1))
If _v Mod 2 = 0 Then
_evens += _v
Else
_odds += _v
End If
Next

Label1.Text = "Sum of odd numbers is: " & _odds.ToString()

Label2.Text = "Sum of even numbers is: " & _evens.ToString()
"Tom Leylan" <tl*****@nospam.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>>Well that will get him a C- :-)

"Lost" <lo*******@mailinator.comwrote in message
news:45***********************@authen.yellow.rea dfreenews.net...
"Ron" <pt*****@yahoo.comwrote:

>I want to write a program that will accept a number in a textbox for
>example 23578 and then in a label will display the sum of the odd and
>even number like this...
>
>the textbox containsthe number 23578
>the label would say:
>Sumof odd number is: 15
>Sum of even number is: 10
>
>any ideas?
>thanks

Dim strNumber As String
Dim strTemp As String
Dim intIndex As Integer
Dim intTotEvens As Integer
Dim intTotOdds As Integer

intTotOdds = 0
intTotEvens = 0
strNumber = Trim(TextBox1.Text) ' contains input, eg 23578
For intIndex = 1 To Len(strNumber)
strTemp = Mid(strNumber, intIndex, 1)
If InStr("02468", strTemp) 0 Then
intTotEvens = intTotEvens + Val(strTemp)
Else
intTotOdds = intTotOdds + Val(strTemp)
End If
Next intIndex
Label1.Text = "Sum of odd numbers is: " + Str(intTotOdds)
Label2.Text = "Sum of even numbers is: " + Str(intTotEvens)

--
Modo vincis, modo peris

Jan 25 '07 #9
I see I forgot to edit my function call... in my test routines I named it
Test7() but I renamed the thing as I posted. And yes I like it as a class.

Is something up with the TotalOddsAndEvens method? It's returning False all
the time and I'm not sure that a non-digit in the string should make it
fail. And since the class is named OddsAndEvens I might suggest "Total()"
is a good enough name for the method.

And this is why I like these simple examples. Note how you grouped the
private variables at the top of the class? It is a very common style and
used in many books, probably in the MS docs as well. I long ago stopped
doing this and opted for a style which came out of Delphi as I recall.

I declare the private instance variable and then the property. That way I
can see things on a "per property" basis and easily determine whether I have
remembered to generate the property, if it is public/private and whether it
is read-only. It's only a style but I've grown to love it... so it would
look like this:

Private m_odds As Int32

Public ReadOnly Property TotalOfOdds() As Int32
Get
Return m_odds
End Get
End Property

Private m_evens As Int32

Public ReadOnly Property TotalOfEvens() As Int32
Get
Return m_evens
End Get
End Property

"Stephany Young" <noone@localhostwrote in message
news:uR*************@TK2MSFTNGP02.phx.gbl...
You're really having fun today Tom! ;)

Shouldn't it be:

' upon some click event or something
Test7(txtInput.Text.Trim())

But now we're really cooking with gas:

Dim oae As New OddsAndEvens

If oae.TotalOddsAndEvens(txtInput.Text.Trim()) Then
lblEven.Text = "Sum of even numbers is: " & oae.TotalOfEvens.ToString()
lblOdd.Text = "Sum of odd numbers is: " & oae.TotalOfOdds.ToString()
Else
MessageBox.Show("Supplied value is not a valid value")
End If

Public Class OddsAndEvens

Private m_odds As Int32
Private m_evens As Int32

Public ReadOnly Property TotalOfOdds() As Int32
Get
Return m_odds
End Get
End Property

Public ReadOnly Property TotalOfEvens() As Int32
Get
Return m_evens
End Get
End Property

Public Function TotalOddsAndEvens(value As String) As Boolean

m_odds = 0
m_evens = 0

Dim nValue As Int32 = 0

For i As Integer = 0 To value.Length - 1
If Int32.TryParse(sInput.Substring(i, 1), nValue)
If IsEven(nValue) Then
nEvens += nValue
ElseIf IsOdd(nValue) Then
nOdds += nValue
End If
Else
Return False
End If
Next

Return False

End Sub

Public Shared Function IsEven(ByVal num As Int32) As Boolean
Return (num Mod 2 = 0)
End Function

Public Shared Function IsOdd(ByVal num As Int32) As Boolean
Return (Not IsEven(num))
End Function

End Class
"Tom Leylan" <tl*****@nospam.netwrote in message
news:er**************@TK2MSFTNGP05.phx.gbl...
>>I guess I sort of stuck my foot in my mouth on that one... now I have to
post something :-) Glad "Lost" took it with humor. What I love about
these homework assignments is that they create small topics from which
solutions can be discussed. Questions like "how do I write a video game"
isn't quite going to be solved by each of us submitting our collision
detection routine.

I'd suggest a couple of things. First the routine should be written
independently of the source of input so I wouldn't grab the data out of
the textbox but would require that to be passed to the routine. I opted
to display the values directly into the labels though. The reason is the
extra code needed to initialize them (outside of the routine) and to pass
them by reference just obfuscates things (since (I doubt) this is going
to be part of a reusable library.)

The other was testing the characters as "strings" is problematic. The
solutions tended to insist if a character wasn't "0,2,4,6 or 8" then it
must have been "1,3,5,7 or 9" and I don't know the user didn't key in
"ab383994*0".

I decided to add some functionality that I might be able to use in the
future by creating IsEven() and IsOdd() functions. While not necessary
I'd hate to write ((value MOD 2) = 0 ) the next time I needed to know.
Uh, let's see I opted for the Int32 declaration (not trying to start a
fight but I prefer it too). And I used TryParse so I can determine when
non-digits were entered and I can ignore them.

So here is mine:

' upon some click event or something
ValueCounter(txtInput.Text.Trim())
Private Sub Test7(ByVal sInput As String)

Dim nEvens As Int32 = 0
Dim nOdds As Int32 = 0

Dim bIsDigit As Boolean
Dim nValue As Int32 = 0

For i As Integer = 0 To (sInput.Length - 1)
bIsDigit = Int32.TryParse(sInput.Substring(i, 1), nValue)
If bIsDigit Then
If IsEven(nValue) Then
nEvens += nValue
ElseIf IsOdd(nValue) Then
nOdds += nValue
End If
End If
Next

lblEven.Text = "Sum of even numbers is: " + nEvens.ToString()
lblOdd.Text = "Sum of odd numbers is: " + nOdds.ToString()

End Sub

Public Function IsEven(ByVal num As Int32) As Boolean
Return ((num Mod 2) = 0)
End Function

Public Function IsOdd(ByVal num As Int32) As Boolean
Return (Not IsEven(num))
End Function

"Stephany Young" <noone@localhostwrote in message
news:us*************@TK2MSFTNGP06.phx.gbl...
>> Dim intTotEvens As Integer
Dim intTotOdds As Integer

Dim strNumber As String = TextBox1.Text.Trim() ' contains input, eg
23578

For intIndex As Integer = 0 To strNumber.Length - 1
Dim strTemp As String = strNumber.SubString(intIndex, 1)
If ("02468").IndexOf(strTemp) >= 0 Then
intTotEvens += Integer.Parse(strTemp)
Else
intTotOdds += Integer.Parse(strTemp)
End If
Next

Label1.Text = "Sum of odd numbers is: " & intTotOdds.ToString()

Label2.Text = "Sum of even numbers is: " & intTotEvens.ToString()

Does that at least get me a C+? :)

Dim _evens As Integer
Dim _odds As Integer

For _i As Integer = 0 To TextBox1.Text.Length - 1
Dim _v As Integer = Integer.Parse(TextBox1.Text.SubString(_i, 1))
If _v Mod 2 = 0 Then
_evens += _v
Else
_odds += _v
End If
Next

Label1.Text = "Sum of odd numbers is: " & _odds.ToString()

Label2.Text = "Sum of even numbers is: " & _evens.ToString()
"Tom Leylan" <tl*****@nospam.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl.. .
Well that will get him a C- :-)

"Lost" <lo*******@mailinator.comwrote in message
"Ron" <pt*****@yahoo.comwrote:
>
>>I want to write a program that will accept a number in a textbox for
>>example 23578 and then in a label will display the sum of the odd and
>>even number like this...
>>
>>the textbox containsthe number 23578
>>the label would say:
>>Sumof odd number is: 15
>>Sum of even number is: 10
>>
>>any ideas?
>>thanks
>
Dim strNumber As String
Dim strTemp As String
Dim intIndex As Integer
Dim intTotEvens As Integer
Dim intTotOdds As Integer
>
intTotOdds = 0
intTotEvens = 0
strNumber = Trim(TextBox1.Text) ' contains input, eg 23578
For intIndex = 1 To Len(strNumber)
strTemp = Mid(strNumber, intIndex, 1)
If InStr("02468", strTemp) 0 Then
intTotEvens = intTotEvens + Val(strTemp)
Else
intTotOdds = intTotOdds + Val(strTemp)
End If
Next intIndex
Label1.Text = "Sum of odd numbers is: " + Str(intTotOdds)
Label2.Text = "Sum of even numbers is: " + Str(intTotEvens)
>
>
>
--
Modo vincis, modo peris

Jan 25 '07 #10
Yes, you're right. The final Return False should be Return True.

Yes, it should fail (return False) if any character cannot be parsed as in
integer. If you supply a value such as 123A456 then that is not a number and
therfore is out of context for the method.

Remember that the OP stated that the user was entering a 'number'.

Of course one could argue that such a function should handle any string that
might represent a number including decimal points, thousands separators,
currency symbols, signs, etc., and, if one was so inclined could easily
expand the functionality to be more 'fuzzy'.

My preference, in a class, is to group the private members so that they are
all in one place.
"Tom Leylan" <tl*****@nospam.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>I see I forgot to edit my function call... in my test routines I named it
Test7() but I renamed the thing as I posted. And yes I like it as a class.

Is something up with the TotalOddsAndEvens method? It's returning False
all the time and I'm not sure that a non-digit in the string should make
it fail. And since the class is named OddsAndEvens I might suggest
"Total()" is a good enough name for the method.

And this is why I like these simple examples. Note how you grouped the
private variables at the top of the class? It is a very common style and
used in many books, probably in the MS docs as well. I long ago stopped
doing this and opted for a style which came out of Delphi as I recall.

I declare the private instance variable and then the property. That way I
can see things on a "per property" basis and easily determine whether I
have remembered to generate the property, if it is public/private and
whether it is read-only. It's only a style but I've grown to love it...
so it would look like this:

Private m_odds As Int32

Public ReadOnly Property TotalOfOdds() As Int32
Get
Return m_odds
End Get
End Property

Private m_evens As Int32

Public ReadOnly Property TotalOfEvens() As Int32
Get
Return m_evens
End Get
End Property

"Stephany Young" <noone@localhostwrote in message
news:uR*************@TK2MSFTNGP02.phx.gbl...
>You're really having fun today Tom! ;)

Shouldn't it be:

' upon some click event or something
Test7(txtInput.Text.Trim())

But now we're really cooking with gas:

Dim oae As New OddsAndEvens

If oae.TotalOddsAndEvens(txtInput.Text.Trim()) Then
lblEven.Text = "Sum of even numbers is: " &
oae.TotalOfEvens.ToString()
lblOdd.Text = "Sum of odd numbers is: " & oae.TotalOfOdds.ToString()
Else
MessageBox.Show("Supplied value is not a valid value")
End If

Public Class OddsAndEvens

Private m_odds As Int32
Private m_evens As Int32

Public ReadOnly Property TotalOfOdds() As Int32
Get
Return m_odds
End Get
End Property

Public ReadOnly Property TotalOfEvens() As Int32
Get
Return m_evens
End Get
End Property

Public Function TotalOddsAndEvens(value As String) As Boolean

m_odds = 0
m_evens = 0

Dim nValue As Int32 = 0

For i As Integer = 0 To value.Length - 1
If Int32.TryParse(sInput.Substring(i, 1), nValue)
If IsEven(nValue) Then
nEvens += nValue
ElseIf IsOdd(nValue) Then
nOdds += nValue
End If
Else
Return False
End If
Next

Return False

End Sub

Public Shared Function IsEven(ByVal num As Int32) As Boolean
Return (num Mod 2 = 0)
End Function

Public Shared Function IsOdd(ByVal num As Int32) As Boolean
Return (Not IsEven(num))
End Function

End Class
"Tom Leylan" <tl*****@nospam.netwrote in message
news:er**************@TK2MSFTNGP05.phx.gbl...
>>>I guess I sort of stuck my foot in my mouth on that one... now I have to
post something :-) Glad "Lost" took it with humor. What I love about
these homework assignments is that they create small topics from which
solutions can be discussed. Questions like "how do I write a video game"
isn't quite going to be solved by each of us submitting our collision
detection routine.

I'd suggest a couple of things. First the routine should be written
independently of the source of input so I wouldn't grab the data out of
the textbox but would require that to be passed to the routine. I opted
to display the values directly into the labels though. The reason is
the extra code needed to initialize them (outside of the routine) and to
pass them by reference just obfuscates things (since (I doubt) this is
going to be part of a reusable library.)

The other was testing the characters as "strings" is problematic. The
solutions tended to insist if a character wasn't "0,2,4,6 or 8" then it
must have been "1,3,5,7 or 9" and I don't know the user didn't key in
"ab383994*0".

I decided to add some functionality that I might be able to use in the
future by creating IsEven() and IsOdd() functions. While not necessary
I'd hate to write ((value MOD 2) = 0 ) the next time I needed to know.
Uh, let's see I opted for the Int32 declaration (not trying to start a
fight but I prefer it too). And I used TryParse so I can determine when
non-digits were entered and I can ignore them.

So here is mine:

' upon some click event or something
ValueCounter(txtInput.Text.Trim())
Private Sub Test7(ByVal sInput As String)

Dim nEvens As Int32 = 0
Dim nOdds As Int32 = 0

Dim bIsDigit As Boolean
Dim nValue As Int32 = 0

For i As Integer = 0 To (sInput.Length - 1)
bIsDigit = Int32.TryParse(sInput.Substring(i, 1), nValue)
If bIsDigit Then
If IsEven(nValue) Then
nEvens += nValue
ElseIf IsOdd(nValue) Then
nOdds += nValue
End If
End If
Next

lblEven.Text = "Sum of even numbers is: " + nEvens.ToString()
lblOdd.Text = "Sum of odd numbers is: " + nOdds.ToString()

End Sub

Public Function IsEven(ByVal num As Int32) As Boolean
Return ((num Mod 2) = 0)
End Function

Public Function IsOdd(ByVal num As Int32) As Boolean
Return (Not IsEven(num))
End Function

"Stephany Young" <noone@localhostwrote in message
news:us*************@TK2MSFTNGP06.phx.gbl...
Dim intTotEvens As Integer
Dim intTotOdds As Integer

Dim strNumber As String = TextBox1.Text.Trim() ' contains input, eg
23578

For intIndex As Integer = 0 To strNumber.Length - 1
Dim strTemp As String = strNumber.SubString(intIndex, 1)
If ("02468").IndexOf(strTemp) >= 0 Then
intTotEvens += Integer.Parse(strTemp)
Else
intTotOdds += Integer.Parse(strTemp)
End If
Next

Label1.Text = "Sum of odd numbers is: " & intTotOdds.ToString()

Label2.Text = "Sum of even numbers is: " & intTotEvens.ToString()

Does that at least get me a C+? :)

Dim _evens As Integer
Dim _odds As Integer

For _i As Integer = 0 To TextBox1.Text.Length - 1
Dim _v As Integer = Integer.Parse(TextBox1.Text.SubString(_i, 1))
If _v Mod 2 = 0 Then
_evens += _v
Else
_odds += _v
End If
Next

Label1.Text = "Sum of odd numbers is: " & _odds.ToString()

Label2.Text = "Sum of even numbers is: " & _evens.ToString()
"Tom Leylan" <tl*****@nospam.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl. ..
Well that will get him a C- :-)
>
"Lost" <lo*******@mailinator.comwrote in message
>"Ron" <pt*****@yahoo.comwrote:
>>
>>>I want to write a program that will accept a number in a textbox for
>>>example 23578 and then in a label will display the sum of the odd and
>>>even number like this...
>>>
>>>the textbox containsthe number 23578
>>>the label would say:
>>>Sumof odd number is: 15
>>>Sum of even number is: 10
>>>
>>>any ideas?
>>>thanks
>>
>Dim strNumber As String
> Dim strTemp As String
> Dim intIndex As Integer
> Dim intTotEvens As Integer
> Dim intTotOdds As Integer
>>
> intTotOdds = 0
> intTotEvens = 0
> strNumber = Trim(TextBox1.Text) ' contains input, eg 23578
> For intIndex = 1 To Len(strNumber)
> strTemp = Mid(strNumber, intIndex, 1)
> If InStr("02468", strTemp) 0 Then
> intTotEvens = intTotEvens + Val(strTemp)
> Else
> intTotOdds = intTotOdds + Val(strTemp)
> End If
> Next intIndex
> Label1.Text = "Sum of odd numbers is: " + Str(intTotOdds)
> Label2.Text = "Sum of even numbers is: " + Str(intTotEvens)
>>
>>
>>
>--
>Modo vincis, modo peris
>
>

Jan 25 '07 #11
thanks for all of yur posts everone. I am not looking for a way to
cheat on this assignmwent, just for some help. And I found it, the
Trim() was what I was looking for, I was not sure how to divide the
number into its individual numbers.

thanks.
On Jan 24, 10:13 pm, "Stephany Young" <noone@localhostwrote:
Yes, you're right. The final Return False should be Return True.

Yes, it should fail (return False) if any character cannot be parsed as in
integer. If you supply a value such as 123A456 then that is not a number and
therfore is out of context for the method.

Remember that the OP stated that the user was entering a 'number'.

Of course one could argue that such a function should handle any string that
might represent a number including decimal points, thousands separators,
currency symbols, signs, etc., and, if one was so inclined could easily
expand the functionality to be more 'fuzzy'.

My preference, in a class, is to group the private members so that they are
all in one place.

"Tom Leylan" <tley...@nospam.netwrote in messagenews:%2****************@TK2MSFTNGP03.phx.gb l...
I see I forgot to edit my function call... in my test routines I named it
Test7() but I renamed the thing as I posted. And yes I like it as a class.
Is something up with the TotalOddsAndEvens method? It's returning False
all the time and I'm not sure that a non-digit in the string should make
it fail. And since the class is named OddsAndEvens I might suggest
"Total()" is a good enough name for the method.
And this is why I like these simple examples. Note how you grouped the
private variables at the top of the class? It is a very common style and
used in many books, probably in the MS docs as well. I long ago stopped
doing this and opted for a style which came out of Delphi as I recall.
I declare the private instance variable and then the property. That way I
can see things on a "per property" basis and easily determine whether I
have remembered to generate the property, if it is public/private and
whether it is read-only. It's only a style but I've grown to love it...
so it would look like this:
Private m_odds As Int32
Public ReadOnly Property TotalOfOdds() As Int32
Get
Return m_odds
End Get
End Property
Private m_evens As Int32
Public ReadOnly Property TotalOfEvens() As Int32
Get
Return m_evens
End Get
End Property
"Stephany Young" <noone@localhostwrote in message
news:uR*************@TK2MSFTNGP02.phx.gbl...
You're really having fun today Tom! ;)
Shouldn't it be:
' upon some click event or something
Test7(txtInput.Text.Trim())
But now we're really cooking with gas:
Dim oae As New OddsAndEvens
If oae.TotalOddsAndEvens(txtInput.Text.Trim()) Then
lblEven.Text = "Sum of even numbers is: " &
oae.TotalOfEvens.ToString()
lblOdd.Text = "Sum of odd numbers is: " & oae.TotalOfOdds.ToString()
Else
MessageBox.Show("Supplied value is not a valid value")
End If
Public Class OddsAndEvens
Private m_odds As Int32
Private m_evens As Int32
Public ReadOnly Property TotalOfOdds() As Int32
Get
Return m_odds
End Get
End Property
Public ReadOnly Property TotalOfEvens() As Int32
Get
Return m_evens
End Get
End Property
Public Function TotalOddsAndEvens(value As String) As Boolean
m_odds = 0
m_evens = 0
Dim nValue As Int32 = 0
For i As Integer = 0 To value.Length - 1
If Int32.TryParse(sInput.Substring(i, 1), nValue)
If IsEven(nValue) Then
nEvens += nValue
ElseIf IsOdd(nValue) Then
nOdds += nValue
End If
Else
Return False
End If
Next
Return False
End Sub
Public Shared Function IsEven(ByVal num As Int32) As Boolean
Return (num Mod 2 = 0)
End Function
Public Shared Function IsOdd(ByVal num As Int32) As Boolean
Return (Not IsEven(num))
End Function
End Class
"Tom Leylan" <tley...@nospam.netwrote in message
news:er**************@TK2MSFTNGP05.phx.gbl...
I guess I sort of stuck my foot in my mouth on that one... now I have to
post something :-) Glad "Lost" took it with humor. What I love about
these homework assignments is that they create small topics from which
solutions can be discussed. Questions like "how do I write a video game"
isn't quite going to be solved by each of us submitting our collision
detection routine.
>I'd suggest a couple of things. First the routine should be written
independently of the source of input so I wouldn't grab the data out of
the textbox but would require that to be passed to the routine. I opted
to display the values directly into the labels though. The reason is
the extra code needed to initialize them (outside of the routine) and to
pass them by reference just obfuscates things (since (I doubt) this is
going to be part of a reusable library.)
>The other was testing the characters as "strings" is problematic. The
solutions tended to insist if a character wasn't "0,2,4,6 or 8" then it
must have been "1,3,5,7 or 9" and I don't know the user didn't key in
"ab383994*0".
>I decided to add some functionality that I might be able to use in the
future by creating IsEven() and IsOdd() functions. While not necessary
I'd hate to write ((value MOD 2) = 0 ) the next time I needed to know.
Uh, let's see I opted for the Int32 declaration (not trying to start a
fight but I prefer it too). And I used TryParse so I can determine when
non-digits were entered and I can ignore them.
>So here is mine:
> ' upon some click event or something
ValueCounter(txtInput.Text.Trim())
> Private Sub Test7(ByVal sInput As String)
> Dim nEvens As Int32 = 0
Dim nOdds As Int32 = 0
> Dim bIsDigit As Boolean
Dim nValue As Int32 = 0
> For i As Integer = 0 To (sInput.Length - 1)
bIsDigit = Int32.TryParse(sInput.Substring(i, 1), nValue)
If bIsDigit Then
If IsEven(nValue) Then
nEvens += nValue
ElseIf IsOdd(nValue) Then
nOdds += nValue
End If
End If
Next
> lblEven.Text = "Sum of even numbers is: " + nEvens.ToString()
lblOdd.Text = "Sum of odd numbers is: " + nOdds.ToString()
> End Sub
> Public Function IsEven(ByVal num As Int32) As Boolean
Return ((num Mod 2) = 0)
End Function
> Public Function IsOdd(ByVal num As Int32) As Boolean
Return (Not IsEven(num))
End Function
>"Stephany Young" <noone@localhostwrote in message
news:us*************@TK2MSFTNGP06.phx.gbl...
Dim intTotEvens As Integer
Dim intTotOdds As Integer
>> Dim strNumber As String = TextBox1.Text.Trim() ' contains input, eg
23578
>> For intIndex As Integer = 0 To strNumber.Length - 1
Dim strTemp As String = strNumber.SubString(intIndex, 1)
If ("02468").IndexOf(strTemp) >= 0 Then
intTotEvens += Integer.Parse(strTemp)
Else
intTotOdds += Integer.Parse(strTemp)
End If
Next
>> Label1.Text = "Sum of odd numbers is: " & intTotOdds.ToString()
>> Label2.Text = "Sum of even numbers is: " & intTotEvens.ToString()
>>Does that at least get me a C+? :)
>> Dim _evens As Integer
Dim _odds As Integer
>> For _i As Integer = 0 To TextBox1.Text.Length - 1
Dim _v As Integer = Integer.Parse(TextBox1.Text.SubString(_i, 1))
If _v Mod 2 = 0 Then
_evens += _v
Else
_odds += _v
End If
Next
>> Label1.Text = "Sum of odd numbers is: " & _odds.ToString()
>> Label2.Text = "Sum of even numbers is: " & _evens.ToString()
>>"Tom Leylan" <tley...@nospam.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl. ..
Well that will get him a C- :-)
>>>"Lost" <lostag...@mailinator.comwrote in message
"Ron" <pts4...@yahoo.comwrote:
>>>>>>I want to write a program that will accept a number in a textbox for
>>example 23578 and then in a label will display the sum of the odd and
>>even number like this...
>>>>>>the textbox containsthe number 23578
>>the label would say:
>>Sumof odd number is: 15
>>Sum of even number is: 10
>>>>>>any ideas?
>>thanks
>>>>Dim strNumber As String
Dim strTemp As String
Dim intIndex As Integer
Dim intTotEvens As Integer
Dim intTotOdds As Integer
>>>> intTotOdds = 0
intTotEvens = 0
strNumber = Trim(TextBox1.Text) ' contains input, eg 23578
For intIndex = 1 To Len(strNumber)
strTemp = Mid(strNumber, intIndex, 1)
If InStr("02468", strTemp) 0 Then
intTotEvens = intTotEvens + Val(strTemp)
Else
intTotOdds = intTotOdds + Val(strTemp)
End If
Next intIndex
Label1.Text = "Sum of odd numbers is: " + Str(intTotOdds)
Label2.Text = "Sum of even numbers is: " + Str(intTotEvens)
>>>>--
Modo vincis, modo peris- Hide quoted text -- Show quoted text -
Jan 25 '07 #12
Well Ron... as you know from reading the docs (I kid, I kid) the Trim()
isn't likely to have any affect whatsoever in 99% of the cases. The user
would have to type in leading or trailing spaces for it to make any impact
at all. I do it as a habit to simply shorten the loop and imbedded blanks
would remain.

If you're really not looking for the code to be written for you then one
alternative is to actually post your code and get it reviewed. And if
really want to know how to program then I'll suggest that when you're
assigned 1 programming assignment you undertake to write 10 programs.
"Ron" <pt*****@yahoo.comwrote in message
thanks for all of yur posts everone. I am not looking for a way to
cheat on this assignmwent, just for some help. And I found it, the
Trim() was what I was looking for, I was not sure how to divide the
number into its individual numbers.

thanks.

Jan 25 '07 #13
"Stephany Young" <noone@localhostwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
My preference, in a class, is to group the private members so that they
are all in one place.
It's the de facto standard and I used to group them as well. But I found
myself having to scroll through the code to find the property itself and had
to work to keep the properties in the same order as the member declarations.
Now I don't. Additionally it should prove useful when dealing with partial
classes.

In any case you (or others) may consider trying it out on a class or two.
If it doesn't work for you don't use it.

Tom
Jan 25 '07 #14

"Stephany Young" <noone@localhostwrote in message
news:uR*************@TK2MSFTNGP02.phx.gbl...
End Class
But Stephany, since this is just a homework assignment, you can only End
Class when the bell rings!!! :)
Jan 25 '07 #15
Tom,

Thanks for this little bit of code. I've been trying to work out in my own
mind how to create a class.

Bruce

"Tom Leylan" <tl*****@nospam.netwrote in message
news:er**************@TK2MSFTNGP05.phx.gbl...
>I guess I sort of stuck my foot in my mouth on that one... now I have to
post something :-) Glad "Lost" took it with humor. What I love about
these homework assignments is that they create small topics from which
solutions can be discussed. Questions like "how do I write a video game"
isn't quite going to be solved by each of us submitting our collision
detection routine.

I'd suggest a couple of things. First the routine should be written
independently of the source of input so I wouldn't grab the data out of
the textbox but would require that to be passed to the routine. I opted
to display the values directly into the labels though. The reason is the
extra code needed to initialize them (outside of the routine) and to pass
them by reference just obfuscates things (since (I doubt) this is going to
be part of a reusable library.)

The other was testing the characters as "strings" is problematic. The
solutions tended to insist if a character wasn't "0,2,4,6 or 8" then it
must have been "1,3,5,7 or 9" and I don't know the user didn't key in
"ab383994*0".

I decided to add some functionality that I might be able to use in the
future by creating IsEven() and IsOdd() functions. While not necessary
I'd hate to write ((value MOD 2) = 0 ) the next time I needed to know.
Uh, let's see I opted for the Int32 declaration (not trying to start a
fight but I prefer it too). And I used TryParse so I can determine when
non-digits were entered and I can ignore them.

So here is mine:

' upon some click event or something
ValueCounter(txtInput.Text.Trim())
Private Sub Test7(ByVal sInput As String)

Dim nEvens As Int32 = 0
Dim nOdds As Int32 = 0

Dim bIsDigit As Boolean
Dim nValue As Int32 = 0

For i As Integer = 0 To (sInput.Length - 1)
bIsDigit = Int32.TryParse(sInput.Substring(i, 1), nValue)
If bIsDigit Then
If IsEven(nValue) Then
nEvens += nValue
ElseIf IsOdd(nValue) Then
nOdds += nValue
End If
End If
Next

lblEven.Text = "Sum of even numbers is: " + nEvens.ToString()
lblOdd.Text = "Sum of odd numbers is: " + nOdds.ToString()

End Sub

Public Function IsEven(ByVal num As Int32) As Boolean
Return ((num Mod 2) = 0)
End Function

Public Function IsOdd(ByVal num As Int32) As Boolean
Return (Not IsEven(num))
End Function

"Stephany Young" <noone@localhostwrote in message
news:us*************@TK2MSFTNGP06.phx.gbl...
> Dim intTotEvens As Integer
Dim intTotOdds As Integer

Dim strNumber As String = TextBox1.Text.Trim() ' contains input, eg
23578

For intIndex As Integer = 0 To strNumber.Length - 1
Dim strTemp As String = strNumber.SubString(intIndex, 1)
If ("02468").IndexOf(strTemp) >= 0 Then
intTotEvens += Integer.Parse(strTemp)
Else
intTotOdds += Integer.Parse(strTemp)
End If
Next

Label1.Text = "Sum of odd numbers is: " & intTotOdds.ToString()

Label2.Text = "Sum of even numbers is: " & intTotEvens.ToString()

Does that at least get me a C+? :)

Dim _evens As Integer
Dim _odds As Integer

For _i As Integer = 0 To TextBox1.Text.Length - 1
Dim _v As Integer = Integer.Parse(TextBox1.Text.SubString(_i, 1))
If _v Mod 2 = 0 Then
_evens += _v
Else
_odds += _v
End If
Next

Label1.Text = "Sum of odd numbers is: " & _odds.ToString()

Label2.Text = "Sum of even numbers is: " & _evens.ToString()
"Tom Leylan" <tl*****@nospam.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>>Well that will get him a C- :-)

"Lost" <lo*******@mailinator.comwrote in message
news:45***********************@authen.yellow.rea dfreenews.net...
"Ron" <pt*****@yahoo.comwrote:

>I want to write a program that will accept a number in a textbox for
>example 23578 and then in a label will display the sum of the odd and
>even number like this...
>
>the textbox containsthe number 23578
>the label would say:
>Sumof odd number is: 15
>Sum of even number is: 10
>
>any ideas?
>thanks

Dim strNumber As String
Dim strTemp As String
Dim intIndex As Integer
Dim intTotEvens As Integer
Dim intTotOdds As Integer

intTotOdds = 0
intTotEvens = 0
strNumber = Trim(TextBox1.Text) ' contains input, eg 23578
For intIndex = 1 To Len(strNumber)
strTemp = Mid(strNumber, intIndex, 1)
If InStr("02468", strTemp) 0 Then
intTotEvens = intTotEvens + Val(strTemp)
Else
intTotOdds = intTotOdds + Val(strTemp)
End If
Next intIndex
Label1.Text = "Sum of odd numbers is: " + Str(intTotOdds)
Label2.Text = "Sum of even numbers is: " + Str(intTotEvens)

--
Modo vincis, modo peris

Jan 25 '07 #16
But only my husband gets to ring my bell ;)
"Bruce W. Darby" <kr******@atcomcast.netwrote in message
news:oM******************************@comcast.com. ..
>
"Stephany Young" <noone@localhostwrote in message
news:uR*************@TK2MSFTNGP02.phx.gbl...
> End Class

But Stephany, since this is just a homework assignment, you can only End
Class when the bell rings!!! :)

Jan 25 '07 #17
BTW,

The help you provided me with my little program for work was greatly
appreciated. It allowed me to advance that project to the point where I was
able to use it at work to show the techs that they don't have to
individually compress all those folders. During the compression, the
interface was non-responsive, however, so I'm going to be trying to put the
compression portion of the module into a thread of it's own. I'm kinda
nervous, but I'm trying to work out a way to use the BackgroundWorker, so
I'll probably be back asking questions again soon. :)

Thanks all,
Bruce
"Stephany Young" <noone@localhostwrote in message
news:eE**************@TK2MSFTNGP03.phx.gbl...
But only my husband gets to ring my bell ;)
"Bruce W. Darby" <kr******@atcomcast.netwrote in message
news:oM******************************@comcast.com. ..
>>
"Stephany Young" <noone@localhostwrote in message
news:uR*************@TK2MSFTNGP02.phx.gbl...
>> End Class

But Stephany, since this is just a homework assignment, you can only End
Class when the bell rings!!! :)

Jan 25 '07 #18

### This discussion thread is closed

Replies have been disabled for this discussion.