Tamer_Ahmed Posted July 20, 2004 Posted July 20, 2004 hi in VB.net i can use ISNumeric to determine if it's numeric or not but in C# i can't use it can any body help Quote
Administrators PlausiblyDamp Posted July 20, 2004 Administrators Posted July 20, 2004 You could use regular expressions to validate the string (search these forums and you'll will find a few samples), or alternatively you may want to use the double.TryParse(...) method - incidently if you look at the VB IsNumeric function in ildasm then it actually calls the TryParse method itself anyway.... Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Jaco Posted July 22, 2004 Posted July 22, 2004 Something like: internal static double IsNumeric(object oValue) { try { if (oValue == null || System.Convert.IsDBNull(oValue)) return false; string sString = oValue.ToString().Trim(); double dValue=0; if (double.TryParse(sString, NumberStyles.Any, CultureInfo.CurrentCulture, out dValue)) return true; else return false; } catch { return false; } } Quote
Mister E Posted August 7, 2004 Posted August 7, 2004 Just reference Microsoft.VisualBasic and you can use the VB IsNumeric function in C#. Otherwise, just do something like this: private bool IsNumeric(object o) { try { double d = Convert.ToDouble(o); return true; } catch { return false; } } Quote
Joe Mamma Posted August 8, 2004 Posted August 8, 2004 Just reference Microsoft.VisualBasic and you can use the VB IsNumeric function in C#. [/Quote]BOO!!!! Bad Vb'er Bad!!! Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
Mister E Posted August 9, 2004 Posted August 9, 2004 It works, and it really doesn't matter since it's all compiled into the same runtime anyway. Quote
jspencer Posted August 9, 2004 Posted August 9, 2004 hi in VB.net i can use ISNumeric to determine if it's numeric or not but in C# i can't use it can any body help Try this out Tamer: Public Overloads Function IsNumeric(ByVal Value As Object) As Boolean Dim RegEx As Regex If Not Value Is Nothing Then RegEx = New Regex("(\+|-)?[0-9][0-9]*(\.[0-9]*)?") If RegEx.IsMatch(Value.ToString) AndAlso RegEx.Match(Value.ToString).Length = CType(Value, String).Length Then Return True Else Return False End If Else Return False End If End Function You should be able to easily convert this to c#. Quote
Administrators PlausiblyDamp Posted August 9, 2004 Administrators Posted August 9, 2004 Although the regex works it does have a locale bias (assumes .) as decimal seperator, and will fail if a thousands seperator is specified (or if a trailing +/- is used). Also it will not cope with currency or non decimal (e.g. hex) numbers. Using double.TryParse like I suggested an Jaco showed how to do in a previous post in this thread is a easy enough way to do this. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.