Questions tagged [tryparse]

Converts the string representation of an object to its typed equivalent. A return value indicates whether the operation succeeded.

Converts the string representation of an object to its typed equivalent. A return value indicates whether the operation succeeded.

The TryParse method is like the Parse method, except the TryParse method does not throw an exception if the conversion fails. It eliminates the need to use exception handling to test for a FormatException in the event that the string is invalid and cannot be successfully parsed.

312 questions
222
votes
25 answers

Generic TryParse

I am trying to create a generic extension that uses 'TryParse' to check if a string is a given type: public static bool Is(this string input) { T notUsed; return T.TryParse(input, out notUsed); } this won't compile as it cannot resolve…
Piers Myers
  • 10,611
  • 6
  • 46
  • 61
120
votes
8 answers

Parse v. TryParse

What is the difference between Parse() and TryParse()? int number = int.Parse(textBoxNumber.Text); // The Try-Parse Method int.TryParse(textBoxNumber.Text, out number); Is there some form of error-checking like a Try-Catch Block?
Kredns
  • 36,461
  • 52
  • 152
  • 203
98
votes
7 answers

DateTime.TryParse issue with dates of yyyy-dd-MM format

I have the following date in string format "2011-29-01 12:00 am" . Now I am trying to convert that to datetime format with the following code: DateTime.TryParse(dateTime, out dt); But I am alwayws getting dt as {1/1/0001 12:00:00 AM} , Can you…
Rocky Singh
  • 15,128
  • 29
  • 99
  • 146
79
votes
8 answers

In C#, how to check whether a string contains an integer?

I just want to know, whether a String variable contains a parsable positive integer value. I do NOT want to parse the value right now. Currently I am doing: int parsedId; if ( (String.IsNullOrEmpty(myStringVariable) || …
Marcel
  • 15,039
  • 20
  • 92
  • 150
72
votes
4 answers

Enum.TryParse returns true for any numeric values

I'm running into a behavior I wasn't expecting when using Enum.TryParse. If I have an enum: public enum MyEnum { ValueA, ValueB, ValueC } And then I pass a numeric value (as a string) into Enum.TryParse, like: MyEnum outputEnum; bool result =…
mweber
  • 1,292
  • 2
  • 10
  • 13
66
votes
9 answers

What is better: int.TryParse or try { int.Parse() } catch

I know.. I know... Performance is not the main concern here, but just for curiosity, what is better? bool parsed = int.TryParse(string, out num); if (parsed) ... OR try { int.Parse(string); } catch () { do something... }
renanleandrof
  • 6,699
  • 9
  • 45
  • 67
52
votes
4 answers

Why do all TryParse overloads have an out parameter?

I have discovered that many times I don't need the out parameter of the TryParse method, but the problem that it is necessarily. Here I will show one example when it's not needed. I want to check if a string is an integer, if it is an integer then…
Misha Zaslavsky
  • 8,414
  • 11
  • 70
  • 116
51
votes
4 answers

Culture invariant Decimal.TryParse()

I'm writing a custom string to decimal validator that needs to use Decimal.TryParse that ignores culture (i.e. doesn't care if the input contains "." or "," as decimal point separator). This is the suggested method: public static bool TryParse( …
Shaggydog
  • 3,456
  • 7
  • 33
  • 50
50
votes
11 answers

How do you test your Request.QueryString[] variables?

I frequently make use of Request.QueryString[] variables. In my Page_load I often do things like: int id = -1; if (Request.QueryString["id"] != null) { try { id =…
Ian G
  • 29,468
  • 21
  • 78
  • 92
50
votes
8 answers

Is there a GUID.TryParse() in .NET 3.5?

UPDATE Guid.TryParse is available in .NET 4.0 END UPDATE Obviously there is no public GUID.TryParse() in .NET CLR 2.0. So, I was looking into regular expressions [aka googling around to find one] and each time I found one there was a heated…
Jack Marchetti
  • 15,536
  • 14
  • 81
  • 117
37
votes
5 answers

What is the correct way to call DateTime.TryParse from F#?

What is the correct way to call DateTime.TryParse from F#? I am trying to test some code from F# interactive and I can't figure out how to pass a mutable DateTime into the second argument by ref. What is the in/out/ref syntax in F#? This is the…
BuddyJoe
  • 69,735
  • 114
  • 291
  • 466
34
votes
8 answers

Is there a Python equivalent to C#'s DateTime.TryParse()?

Is there an equivalent to C#'s DateTime.TryParse() in Python? I'm referring to the fact that it avoids throwing an exception, not the fact that it guesses the format.
user541686
  • 205,094
  • 128
  • 528
  • 886
34
votes
7 answers

Integer.TryParse - a better way?

I find myself often needing to use Integer.TryParse to test if a value is an integer. However, when you use TryParse, you have to pass a reference variable to the function, so I find myself always needing to create a blank integer to pass in. …
Ryan Smith
  • 8,344
  • 22
  • 76
  • 103
29
votes
6 answers

Safely converting string to bool in PowerShell

I'm trying to convert an argument of my PowerShell script to a boolean value. This line [System.Convert]::ToBoolean($a) works fine as long as I use valid values such as "true" or "false", but when an invalid value, such as "bla" or "" is passed, an…
Shaggydog
  • 3,456
  • 7
  • 33
  • 50
27
votes
7 answers

Make TryParse compatible with comma or dot decimal separator

The problem: Let's assume you are using a dot "." as a decimal separator in your regional setting and have coded a string with a comma. string str = "2,5"; What happens when you decimal.TryParse(str, out somevariable); it? somevariable will assume…
ng80092b
  • 621
  • 1
  • 9
  • 24
1
2 3
20 21