|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
.NET equivilant of isnumericWhat'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 "moondaddy" <moondaddy@noemail.noemail> schrieb: Why not? Yes, there are possible solutions based on regular expressions or > 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 VB's 'Like' operator, but why bother with implementing your own solution based on the classes in the .NET Framework if this has already been done by somebody else? Note that 'IsNumeric' is not suitable to check if the user has entered a number in most scenarios because it does not guarantee the number represented by the string can be stored in a variable of a certain numeric data type. If you want to parse user input into a certain numeric type, you may want to use the type's 'Parse' or 'TryParse' method. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Show quote
Hide quote
>> What's the .net framework equivalent of the vb function isnumeric? Since IsNumeric is basically a wrapper around TryParse (with a fair amount >> 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 >> > Why not? Yes, there are possible solutions based on regular > expressions or VB's 'Like' operator, but why bother with implementing > your own solution based on the classes in the .NET Framework if this > has already been done by somebody else? > > Note that 'IsNumeric' is not suitable to check if the user has entered > a number in most scenarios because it does not guarantee the number > represented by the string can be stored in a variable of a certain > numeric data type. If you want to parse user input into a certain > numeric type, you may want to use the type's 'Parse' or 'TryParse' > method. of type checking), I'm not sure where you would consider it inferior. I have noticed that some of the TryParse methods have issues with extra symbols (like the currency ones) if you use the default method. I would recommend using the overloads if you need to be sure you are getting the correct results. See http://devauthority.com/blogs/jwooley/archive/2006/03/15/788.aspx for Jim Wooley http://devauthority.com/blogs/jwooley/default.aspx OK thanks for everyone's feedback above. looks like I'll stick with
isnumeric. -- Show quoteHide quotemoondaddy@noemail.noemail "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message news:O7Nbo8hxGHA.4444@TK2MSFTNGP02.phx.gbl... > "moondaddy" <moondaddy@noemail.noemail> schrieb: >> 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 > > Why not? Yes, there are possible solutions based on regular expressions > or VB's 'Like' operator, but why bother with implementing your own > solution based on the classes in the .NET Framework if this has already > been done by somebody else? > > Note that 'IsNumeric' is not suitable to check if the user has entered a > number in most scenarios because it does not guarantee the number > represented by the string can be stored in a variable of a certain numeric > data type. If you want to parse user input into a certain numeric type, > you may want to use the type's 'Parse' or 'TryParse' method. > > -- > M S Herfried K. Wagner > M V P <URL:http://dotnet.mvps.org/> > V B <URL:http://dotnet.mvps.org/dotnet/faqs/> I second what Herfried says about IsNumeric.
It's a perfectly legit part of the framework and far better tested than anything you could substitute. -- Show quoteHide quoteDavid Anton www.tangiblesoftwaresolutions.com Instant C#: VB to C# converter Instant VB: C# to VB converter Instant C++: C#/VB to C++ converter C# Code Metrics: Quick metrics for C# "moondaddy" wrote: > 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 > > > moondaddy wrote:
> What's the .net framework equivalent of the vb function isnumeric? If there If you're writing in Visual Basic, there are just some things you can't > 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 avoid coding in a Visual Basic way. IsNumeric, vbTab, Redim Preserve; the list goes on. if you're really set on it, though, your "non-VB" alternative would be something like: Try ' substitute Type as required [Integer].Parse( "value" ) ' Yes, it's an Integer Catch System.FormatException ' No, it's not End Try More efficient? Doubt it. Easier to read? Nope. Progress? I'll let you make up your own mind. HTH, Phill W.
Show quote
Hide quote
>> What's the .net framework equivalent of the vb function isnumeric? Using .TryParse over IsNumeric has it's advantages. Particularly in cases >> 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 >> > If you're writing in Visual Basic, there are just some things you > can't avoid coding in a Visual Basic way. IsNumeric, vbTab, Redim > Preserve; the list goes on. > > if you're really set on it, though, your "non-VB" alternative would be > something like: > > Try > ' substitute Type as required > [Integer].Parse( "value" ) > ' Yes, it's an Integer > Catch System.FormatException > ' No, it's not > End Try > More efficient? Doubt it. > Easier to read? Nope. > Progress? I'll let you make up your own mind. > HTH, > Phill W. where you want to use the number after you are sure that it is a number. IsNumeric wraps TryParse. It just throws away the result. If you need ultra performance, use TryParse natively and use the results rather than throwing them away and getting them back again (possibly with CInt). Then again, if you just want to make sure that it is a number, but not process the value, IsNumeric is fine. Jim Wooley http://devauthority.com/blogs/jwooley/default.aspx Jim,
A user who moves the form one pixel on the screen has probably used more processor power than your solution will give back in 100 years using your tryparse solution above the isnumeric. I have seen these newsgroup full of people writting to keep the UI thread alive by creating background processes. In your option you should freeze the UI consequently. Just my thought reading your message. Cor Show quoteHide quote "Jim Wooley" <jimNOSPAMwooley@hotmail.com> schreef in bericht news:24f81e8fbd498c894ac61ab659b@msnews.microsoft.com... >>> 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 >>> >> If you're writing in Visual Basic, there are just some things you >> can't avoid coding in a Visual Basic way. IsNumeric, vbTab, Redim >> Preserve; the list goes on. >> >> if you're really set on it, though, your "non-VB" alternative would be >> something like: >> >> Try >> ' substitute Type as required >> [Integer].Parse( "value" ) >> ' Yes, it's an Integer >> Catch System.FormatException >> ' No, it's not >> End Try >> More efficient? Doubt it. >> Easier to read? Nope. >> Progress? I'll let you make up your own mind. >> HTH, >> Phill W. > > Using .TryParse over IsNumeric has it's advantages. Particularly in cases > where you want to use the number after you are sure that it is a number. > IsNumeric wraps TryParse. It just throws away the result. If you need > ultra performance, use TryParse natively and use the results rather than > throwing them away and getting them back again (possibly with CInt). Then > again, if you just want to make sure that it is a number, but not process > the value, IsNumeric is fine. > > Jim Wooley > http://devauthority.com/blogs/jwooley/default.aspx > > "Jim Wooley" <jimNOSPAMwooley@hotmail.com> schrieb: It doesn't wrap a .NET Framework type's 'TryParse' method.> Using .TryParse over IsNumeric has it's advantages. Particularly in cases > where you want to use the number after you are sure that it is a number. > IsNumeric wraps TryParse. It just throws away the result. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Conversion
DLL created in .NET cannot use? (Inno Setup) Drawing images in different shapes VB.Net Merging Datasets Incorrect behavior of IIf in VB.NET (.NET Framework 1.1 SP1) can I use Imports with a VB2005 class namespace? Trouble Instanciating Structures change gridview text Lines disappear when minimizing or other windows are placed on top of it. Remove x number of charaters from string. Vb.net |
|||||||||||||||||||||||