473,385 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

"If" statement asking for integer

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.

Sep 1 '06 #1
6 2362
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


Ja**********@ec.gc.ca napisal(a):
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.
Sep 1 '06 #2

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
"Ja**********@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.

Sep 1 '06 #3
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
Sep 1 '06 #4
<Ja**********@ec.gc.caschrieb:
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/>
Sep 1 '06 #5
Hi

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

Cheers
Martin
Herfried K. Wagner [MVP] wrote:
<Ja**********@ec.gc.caschrieb:
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/>
Sep 1 '06 #6
Ja**********@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.
Sep 1 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Scott Beavers | last post by:
I'm trying to create a form in Excel to sort from the form and take the data to another worksheet. I am very new to this and any help would be appreciated. I have a value in a cell that will...
3
by: Chris Tilley - HPC:Factor | last post by:
Hi, I'm utterly confounded by this one. There must be some sort of rule that I don't know of. I'd consider myself a Newbie+1, so be gentle. I have a database connection (working A-Ok) and a...
1
by: M Wells | last post by:
Hi All, Just wondering if anyone can tell me if you can test for multiple conditions as part of an "IF" statement in T-SQL in SQL Server 2000? ie something like: IF @merr = 1 or @merr=2...
6
by: JS | last post by:
In this sample code: if(n==0&&args>1){ for(i=num;args>i+1;i++){ s.length = 0; opt = document.createElement('OPTION'); s.appendChild(opt); opt.value = ""; opt.text = "\74-- Vælg --"; }
35
by: David Cleaver | last post by:
Hello all, I was wondering if there were some sort of limitations on the "if" statement? I'm writing a program which needs to check a bunch of conditions all at the same time (basically). And...
32
by: Stephen | last post by:
Is there a standard way to remove the warning that a C compiler might produce from the statement: if (a = b) {} I don't want to do: if ((a = b) != 0) {} Because my "a = b" is actually...
12
by: John | last post by:
I can't get my head around this! I have the following code: <% .... Code for connection to the database ... .... Code for retrieving recordset ... If Not rs.EOF Then ... Do something...
2
by: marsarden | last post by:
write code like: int main(void) { int a=10; if(a<20) {} } Compiler ok on dev-cpp . don't we have to add a ";" after if
6
by: vl106 | last post by:
A static code analysis tool gave me a warning on if (ptr && ptr->data) { ... } I assumed the tool doesn't get the "short circuit behaviour" in the if statement. But a collegue said it may...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.