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

If you don’t want the exception, catch the exception. try: d = datetime.datetime.strptime(s, “%Y-%m-%d %H:%M:%S”) except ValueError: d = None In the zen of Python, explicit is better than implicit. strptime always returns a datetime parsed in the exact format specified. This makes sense, because you have to define the behavior in case of failure, … Read more

Integer.TryParse – a better way?

No need to declare the integer. If Integer.TryParse(intToCheck, 0) Then or If Integer.TryParse(intToCheck, Nothing) Then If you have .Net 3.5 ability you can create an extension method for strings. Public Module MyExtensions <System.Runtime.CompilerServices.Extension()> _ Public Function IsInteger(ByVal value As String) As Boolean If String.IsNullOrEmpty(value) Then Return False Else Return Integer.TryParse(value, Nothing) End If End Function … Read more

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

Chris’s answer is correct if you really need to pass a mutable DateTime by reference. However, it is much more idiomatic in F# to use the compiler’s ability to treat trailing out parameters as tupled return values: let couldParse, parsedDate = System.DateTime.TryParse(“11/27/2012”) Here, the first value is the bool return value, while the second is … Read more

Culture invariant Decimal.TryParse()

In fact CultureInfo.InvariantCulture can be used here. The parameter expects IFormatProvider, an interface that CultureInfo implements. But InvariantCulture is invariant in the sense that it does not vary with the user’s settings. In fact, there is no culture that accepts either , or . as decimal separator – they are all one or the other. … Read more

Why do all TryParse overloads have an out parameter? [closed]

Updated Answer: In more recent versions of C# you can declare the output parameter inline, which allows you to remove the line of code you don’t want in your example: string value = Console.ReadLine(); //Get a value from the user. if (int.TryParse(value, out int num)) { Console.WriteLine(“An integer”); } else { Console.WriteLine(“Not an integer”); } … Read more

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

Below is an extension method that will allow you to write code like this: int id = request.QueryString.GetValue<int>(“id”); DateTime date = request.QueryString.GetValue<DateTime>(“date”); It makes use of TypeDescriptor to perform the conversion. Based on your needs, you could add an overload which takes a default value instead of throwing an exception: public static T GetValue<T>(this NameValueCollection … Read more

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

Better is highly subjective. For instance, I personally prefer int.TryParse, since I most often don’t care why the parsing fails, if it fails. However, int.Parse can (according to the documentation) throw three different exceptions: the input is null the input is not in a valid format the input contains a number that produces an overflow … Read more

Enum.TryParse returns true for any numeric values

This behavior is by design. The documentation says: . If value is the string representation of an integer that does not represent an underlying value of the TEnum enumeration, the method returns an enumeration member whose underlying value is value converted to an integral type. If this behavior is undesirable, call the IsDefined method to … Read more

techhipbettruvabetnorabahisbahis forumu