473,804 Members | 3,185 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

IsNumeric (VB) equivalent in C#

Hi,

I am new to C#, and I am a VB programmer. I would like to know whether there
is any function in C# that can is similar to IsNumeric in VB?
Apr 11 '06 #1
9 8984
You can use Char.IsNumber(c har) or RegEx to write your own function to check
the string IsNumeric or not. Or you can reference to the
Microsoft.Visua lBasic to use the function IsNumeric.

function bool IsNumeric(strin g input)
{
bool result = true;
for (int i = 0; i < input.Length; i++)
{
if (!Char.IsNumber (input[i]))
{
result = false;
break;
}
}
return result;
}

"Kimelia Schiles" wrote:
Hi,

I am new to C#, and I am a VB programmer. I would like to know whether there
is any function in C# that can is similar to IsNumeric in VB?

Apr 11 '06 #2
Or you can use try, catch to know string is numeric or not:

public bool IsNumeric(strin g s)
{
try {
Int32.Parse(s);
}
catch {
return false;
}
return true;
}

or you can try RegEx:

private static Regex RegExIsNumber = new Regex(@"^\d+$") ;

public static bool IsInteger(strin g input)
{
Match m = RegExIsNumber.M atch(input);
return m.Success;
}
Hope this will help you
"Kimelia Schiles" wrote:
Hi,

I am new to C#, and I am a VB programmer. I would like to know whether there
is any function in C# that can is similar to IsNumeric in VB?

Apr 11 '06 #3
There is also Double.TryParse which will never throw an exception, but merely return false if it fails to parse.
On Tue, 11 Apr 2006 05:26:02 +0200, Minh Nguyen <Mi********@dis cussions.micros oft.com> wrote:
Or you can use try, catch to know string is numeric or not:

public bool IsNumeric(strin g s)
{
try {
Int32.Parse(s);
}
catch {
return false;
}
return true;
}

or you can try RegEx:

private static Regex RegExIsNumber = new Regex(@"^\d+$") ;

public static bool IsInteger(strin g input)
{
Match m = RegExIsNumber.M atch(input);
return m.Success;
}
Hope this will help you
"Kimelia Schiles" wrote:
Hi,

I am new to C#, and I am a VB programmer. I would like to know whether there
is any function in C# that can is similar to IsNumeric in VB?


--
Happy coding!
Morten Wennevik [C# MVP]
Apr 11 '06 #4
Your function will fail on several different string formats of numbers, such
as:

0xA2B34
3.14159
123E+11
5,000

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

"Minh Nguyen" <Mi********@dis cussions.micros oft.com> wrote in message
news:A0******** *************** ***********@mic rosoft.com...
You can use Char.IsNumber(c har) or RegEx to write your own function to
check
the string IsNumeric or not. Or you can reference to the
Microsoft.Visua lBasic to use the function IsNumeric.

function bool IsNumeric(strin g input)
{
bool result = true;
for (int i = 0; i < input.Length; i++)
{
if (!Char.IsNumber (input[i]))
{
result = false;
break;
}
}
return result;
}

"Kimelia Schiles" wrote:
Hi,

I am new to C#, and I am a VB programmer. I would like to know whether
there
is any function in C# that can is similar to IsNumeric in VB?

Apr 11 '06 #5
I tend to use Double.TryParse , except when a number falls outside of the
range of double. It works almost across the board.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

"Morten Wennevik" <Mo************ @hotmail.com> wrote in message
news:op.s7ttm4a 5klbvpo@stone.. .
There is also Double.TryParse which will never throw an exception, but
merely return false if it fails to parse.

Apr 11 '06 #6
There is single method call in the .NET Framework that will do this.
Check out this link for a typical method that will simulate IsNumeric
reasonably closely.
http://www.tangiblesoftwaresolutions...0IsNumeric.htm
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter

"Kimelia Schiles" wrote:
Hi,

I am new to C#, and I am a VB programmer. I would like to know whether there
is any function in C# that can is similar to IsNumeric in VB?

Apr 11 '06 #7
That should have been "There is *no* single method call..."
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter

"David Anton" wrote:
There is single method call in the .NET Framework that will do this.
Check out this link for a typical method that will simulate IsNumeric
reasonably closely.
http://www.tangiblesoftwaresolutions...0IsNumeric.htm
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter & VB to C++ converter
Instant J#: VB to J# converter

"Kimelia Schiles" wrote:
Hi,

I am new to C#, and I am a VB programmer. I would like to know whether there
is any function in C# that can is similar to IsNumeric in VB?

Apr 11 '06 #8
On Tue, 11 Apr 2006 10:34:19 +0800, "Kimelia Schiles" <ka**@hotmail.c om>
wrote:
Hi,

I am new to C#, and I am a VB programmer. I would like to know whether there
is any function in C# that can is similar to IsNumeric in VB?


I assume you're not using VS.Net 2005. Anyway, you can actually use a lot of
your intrinsic VB stuff in C#.

Create a console project and set a reference to Microsoft.Visua lBasic
using System;
using System.Text;
using Microsoft.Visua lBasic;

namespace usingvbtest
{
class IsNumericTest
{
static void Main()
{
String str = "123456";
Console.WriteLi ne("{0}: {1}", str, Information.IsN umeric(str));
}
}
}

Alan
Apr 12 '06 #9
> Create a console project and set a reference to Microsoft.Visua lBasic

In the case of IsNumeric, this is actually a pretty good idea in many cases.
The VB IsNumeric intrinsic function is pretty nicely designed. It is
culture-sensitive, and returns true if the data type is Boolean, Byte,
Decimal, Double, Integer, Long, SByte, Short, Single, UInteger, ULong, or
UShort. It also returns true if the parameter passed is a Char, String, or
Object that can be successfully converted to a number.

In short, if little is known about the data being evaluated, this can be
just the ticket. OTOH, it will not be as fast as, for example,
Double.TryParse , which can be used if the expected value falls withing
certain parameters and/or something is known about the data being evaluated.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.

"Alan Popow" <al*****@sympat ico.ca> wrote in message
news:ma******** *************** *********@4ax.c om...
On Tue, 11 Apr 2006 10:34:19 +0800, "Kimelia Schiles" <ka**@hotmail.c om>
wrote:
Hi,

I am new to C#, and I am a VB programmer. I would like to know whether
there
is any function in C# that can is similar to IsNumeric in VB?


I assume you're not using VS.Net 2005. Anyway, you can actually use a lot
of
your intrinsic VB stuff in C#.

Create a console project and set a reference to Microsoft.Visua lBasic
using System;
using System.Text;
using Microsoft.Visua lBasic;

namespace usingvbtest
{
class IsNumericTest
{
static void Main()
{
String str = "123456";
Console.WriteLi ne("{0}: {1}", str, Information.IsN umeric(str));
}
}
}

Alan

Apr 12 '06 #10

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

Similar topics

3
5209
by: Joel | last post by:
In VBA, you can use IsNumeric to return True or False if the value being tested is numeric. Is there an equivalent to this in VB.NET?
7
8342
by: Stephan Bour | last post by:
IS there a C# equivalent to the VB IsNumeric function?
7
1908
by: Joe | last post by:
Hello All: Does anyone know how someone would check for a numeric value in .NET? In VB6, we used IsNumeric. What's the .NET equivalent? BTW, I know that I can harken back to IsNumeric. I'm trying to purge any trace of VB6 from my code. TIA, --
7
1901
by: Vikas Kumar | last post by:
For Each item In Request.Form If IsNumeric(item) Then Try Approved(item) Catch ex As Exception Response.Write(ex.Message) End Try End If Next
3
1363
by: Vikas Kumar | last post by:
For Each item In Request.Form If IsNumeric(item) Then Try Approved(item) Catch ex As Exception Response.Write(ex.Message) End Try End If Next
8
2591
by: moondaddy | last post by:
What's the .net framework equivalent of the vb function isnumeric? If there isn't one, how can I test a string variable to see if its a number or not? I don't want to use isnumeric if possible -- moondaddy@noemail.noemail
12
1668
by: Paul | last post by:
Hi, I am trying to check a string to see if it's first 3 characters are numeric and if they are, to replace those 3 characters with something else. I've tried this but nothing happens... newname = Replace(newname, VB.Left(newname, 3) = "101-", "101. ")
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9572
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
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
10303
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
10070
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
9132
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
6845
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
5508
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2978
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.