"If" statement asking for integer

  • Thread starter Thread starter Jacob.Bruxer
  • Start date Start date
J

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.
 
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




(e-mail address removed) napisal(a):
 
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,
 
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
 
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...'
 
Hi

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

Cheers
Martin
 
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.
 
Back
Top