473,769 Members | 5,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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 2379
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**********@e c.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 ArgumentExcepti on()
End If

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx
Sep 1 '06 #4
<Ja**********@e c.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**********@e c.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(send er, 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
10899
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 direct what sheet I want the cells copied to, so I thought I could use an If statement but have been unable to right the code properly, ie... If "f2" = 1 Then Sheets("XX1").Select
3
1766
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 Query String value from a form (passed using GET & working Ok). The server is IIS5. What I'm trying to do is get a simple IF statement to work accross the two numeric values. Once from a database and one from a Query String.
1
66870
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 Begin SELECT statement
6
2269
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
2738
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 I'm pretty sure the rest of the program is working just fine. The only thing I could think might be wrong is that the if statement can only hold so many values in itself? Let me show what I'm doing: if (table001]>>5]&b&0x1f] != 0 &&
32
2359
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 contained in a macro that might be used
12
20703
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
2073
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
2254
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 be the missing check ot ptr against NULL.
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.