Odd or Even

W

William Foster

Good evening all,

Does anyone know how to find out whether a value is either odd or even
in the Visual Basic component of Visual Studio 2005. I have found the
Math and Information tools which provide some of the common functions
within other Microsoft packages such as Excel; however basic functions
like Odd and Even elude me.

Any assistance you may be able to provide would be greatly appreciated.

Yours sincerely,

William Foster
 
W

William Foster

Ken,

Thank you for the quick reply, you have no idea how many silly
references there are to odd and even within the Visual Studio help, it
was driving me quite mad.

Yours sincerely,

William Foster
 
H

Herfried K. Wagner [MVP]

William Foster said:
Does anyone know how to find out whether a value is either odd or even
in the Visual Basic component of Visual Studio 2005. I have found the
Math and Information tools which provide some of the common functions
within other Microsoft packages such as Excel; however basic functions
like Odd and Even elude me.

\\\
If i Mod 2 = 0 Then
MsgBox("Even.")
Else
MsgBox("Odd.")
End If
///
 
W

William Foster

Herfried,

Thanks for your idea, Ken nailed the solution I needed first go though.

I should have written the question a couple of hours ago instead of
getting mad at the endless pages of help on functions that don't exist
in this suite.

Yours sincerely,

William Foster
 
G

Guest

or simply

Public Function IsOdd(value as Integer) as boolean
Return CBool(value And 1)
End Function

*guy*
 
S

Scott M.

What does this do? the expression "value AND 1" doesn't seem to have any
meaning. In an If statement you need a complete expression to have any
meaning (ie. value<5 AND 1>0). "value AND 1" will always return true, since
value is always = value and 1 is always = 1.

The Modulus (Mod) mathematical operand is designed to divide 2 numbers and
return the remainder, so taking a number and using mod 2 will indicate if
you have an even number if there is no remainder.
 
R

Rocky

What does this do? the expression "value AND 1" doesn't seem to have any
meaning. In an If statement you need a complete expression to have any
meaning (ie. value<5 AND 1>0). "value AND 1" will always return true,
since value is always = value and 1 is always = 1.

Huh? He was testing if the LSB (Least Significant Bit) was set.
 
G

Guest

hi scott, and is used here as a bit operator

basically take two integers and the result has a bit set if the same bit is
set in both operators

with a logical 'Or' the result has a bit set if a bit is set in either
operator, see also Xor, Not ....

hth
*guy*
 
H

Herfried K. Wagner [MVP]

Scott M. said:
What does this do? the expression "value AND 1" doesn't seem to have any
meaning. In an If statement you need a complete expression to have any
meaning (ie. value<5 AND 1>0). "value AND 1" will always return true,
since value is always = value and 1 is always = 1.

F1, 'And' ;-).

'And' is a binary operator which will use a binary AND to combine both
'value' and '1'. The result is converted to a 'Boolean'.
 
S

Scott M.

Ahhh!

Thanks.


guy said:
hi scott, and is used here as a bit operator

basically take two integers and the result has a bit set if the same bit
is
set in both operators

with a logical 'Or' the result has a bit set if a bit is set in either
operator, see also Xor, Not ....

hth
*guy*
 
C

C-Services Holland b.v.

Steve said:
Or before mod was invented...

if int(value)/2=value/2 then isven=true

-sw

You might get problems with that because 2=2.0 can result in false.
Therefore I've always used bit comparison like guy posted.
 
C

Chris Dunaway

I think a more appropriate method would be this:

Public Function IsOdd(value As Integer) As Boolean
Return ((value And 1) = 1)
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Visual Basic .NET Compiler 5
Creating a Trial Period 6
Experience Request: GPS Application 6
CSV File Import 11
Debug Assistance 2
DateTime conversion to Integer 8
StatusBar Replacement 1
Function with Two Values 6

Top