Home All Groups Group Topic Archive Search About

"If" statement asking for integer

Author
1 Sep 2006 12:37 PM
Jacob.Bruxer
Hi,
I'm pretty new to Visual Basic and programming in general.  I want to
know if it's possible to create an If statement that asks if a value is
an integer.  If it's an integer it does one thing, if it's a decimal
value it does something else.  Something like the following...

If i = integer Then
..... a....
Else
...... b .....
EndIf

Seems like it should be fairly simple to me.  Any help would be
appreciated, thanks.

Author
1 Sep 2006 12:47 PM
Mrozu
Hi,

If is integer or decimal? I don't know...

but you can use

If IsNumeric(value)

If NOT IsNumeric(value)


code in VB.NET of kors


Mrozu




Jacob.Bru***@ec.gc.ca napisal(a):
Show quoteHide quote
> Hi,
> I'm pretty new to Visual Basic and programming in general.  I want to
> know if it's possible to create an If statement that asks if a value is
> an integer.  If it's an integer it does one thing, if it's a decimal
> value it does something else.  Something like the following...
>
> If i = integer Then
> .... a....
> Else
> ..... b .....
> EndIf
>
> Seems like it should be fairly simple to me.  Any help would be
> appreciated, thanks.
Author
1 Sep 2006 1:04 PM
Helen Trim
I don't know if there is a more elegant way, but this will work:

if cdbl(cint(i)) = i then
   ' i is an integer
else
   ' i is not an integer
endif

If i = 5.2, say, then cint(i) gives 5, cdbl(cint(i)) gives 5.0 and the test
fails.

I hope this helps,
--
Helen


Show quoteHide quote
"Jacob.Bru***@ec.gc.ca" wrote:

> Hi,
> I'm pretty new to Visual Basic and programming in general.  I want to
> know if it's possible to create an If statement that asks if a value is
> an integer.  If it's an integer it does one thing, if it's a decimal
> value it does something else.  Something like the following...
>
> If i = integer Then
> ..... a....
> Else
> ...... b .....
> EndIf
>
> Seems like it should be fairly simple to me.  Any help would be
> appreciated, thanks.
>
>
Author
1 Sep 2006 1:05 PM
Jim Wooley
Show quote Hide quote
> Hi,
> I'm pretty new to Visual Basic and programming in general.  I want to
> know if it's possible to create an If statement that asks if a value
> is
> an integer.  If it's an integer it does one thing, if it's a decimal
> value it does something else.  Something like the following...
> If i = integer Then
> .... a....
> Else
> ..... b .....
> EndIf
> Seems like it should be fairly simple to me.  Any help would be
> appreciated, thanks.
>

If TypeOf i is Integer then
' do something
ElseIf TypeOf i is Decimal then
' do something
Else
  Throw New ArgumentException()
End If

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx
Author
1 Sep 2006 1:13 PM
Herfried K. Wagner [MVP]
<Jacob.Bru***@ec.gc.ca> schrieb:
> I'm pretty new to Visual Basic and programming in general.  I want to
> know if it's possible to create an If statement that asks if a value is
> an integer.  If it's an integer it does one thing, if it's a decimal
> value it does something else.  Something like the following...
>
> If i = integer Then
> .... a....
> Else
> ..... b .....
> EndIf

'If d = CInt(d) Then...'

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
1 Sep 2006 1:27 PM
Pritcham
Hi

If you're using .Net 2 then you could try using
Int32.TryParse(yourvalue)

Cheers
Martin


Herfried K. Wagner [MVP] wrote:
Show quoteHide quote
> <Jacob.Bru***@ec.gc.ca> schrieb:
> > I'm pretty new to Visual Basic and programming in general.  I want to
> > know if it's possible to create an If statement that asks if a value is
> > an integer.  If it's an integer it does one thing, if it's a decimal
> > value it does something else.  Something like the following...
> >
> > If i = integer Then
> > .... a....
> > Else
> > ..... b .....
> > EndIf
>
> 'If d = CInt(d) Then...'
>
> --
>  M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
1 Sep 2006 3:41 PM
Phill W.
Jacob.Bru***@ec.gc.ca wrote:

> I'm pretty new to Visual Basic and programming in general.  I want to
> know if it's possible to create an If statement that asks if a value is
> an integer. 

Do you mean
(a) The /value/ in a variable is an integer?

If i = Int( i ) Then
    ...

(b) The /Type/ of the variable holding the value is an Integer?

If TypeOf i Is Integer Then
    ...

The latter test extends to all data types, including you own classes, as in

If TypeOf sender Is CustomListView Then
    With DirectCast(sender, CustomListView)
       ...
    End With
    ...

HTH,
    Phill  W.