tryparse

int.TryParse = null if not numeric?

那年仲夏 提交于 2019-12-04 00:05:59
is there some way of return null if it can't parse a string to int? with: public .... , string? categoryID) { int.TryParse(categoryID, out categoryID); getting "cannot convert from 'out string' to 'out int' what to do? EDIT: No longer relevant because of asp.net constraints is the way to solve problem /M First of all, why are you trying to parse a string to an int and stick the result back into a string? The method signature is bool int.TryParse(string, out int) so you have to give a variable of type int as second argument. This also means that you won't get null if parsing fails, instead the

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

断了今生、忘了曾经 提交于 2019-12-03 23:19:39
问题 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. 回答1: 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

Decimal.TryParse doesn't parse my decimal value

巧了我就是萌 提交于 2019-12-03 22:31:43
When I tried to convert something like 0.1 (from user in textbox), My value b is always false. bool b = Decimal.TryParse("0.1", out value); How can it be here to work? Too late to the party, but I was going to suggest forcing the culuture to en-US but Invariant is a better sln decimal value; bool b = Decimal.TryParse("0.1", NumberStyles.Any, new CultureInfo("en-US"), out value); Specify the culture for the parsing. Your current culture uses some different number format, probably 0,1 . This will successfully parse the string: bool b = Decimal.TryParse("0.1", NumberStyles.Any, CultureInfo

valid date check with DateTime.TryParse method

匆匆过客 提交于 2019-12-03 19:55:55
问题 I am using Datetime.TryParse method to check the valid datetime. the input date string would be any string data. but is returning false as the specify date in invalid. DateTime fromDateValue; if (DateTime.TryParse("15/07/2012", out fromDateValue)) { //do for valid date } else { //do for in-valid date } Edit: i missed. i need to check the valid date with time as "15/07/2012 12:00:00". Any suggestions are welcome.... 回答1: You could use the TryParseExact method which allows you to pass a

Decimal TryParse in VBA

老子叫甜甜 提交于 2019-12-02 19:57:30
问题 I am attempting to tryparse using decimal; however, I keep getting an "Object Required" run-time error. I'm not certain what I'm doing wrong. I'm used to doing a tryparse in C#. This is VBA, so the language translation is not clicking just yet. Any help appreciated. Sub try() Dim val As Variant Dim res As Boolean res = Decimal.TryParse("2.5", val) MsgBox (res & ":" & val) End Sub 回答1: You can try CInt and check for a specific error using On Error Goto. 回答2: res = cBool(Val("2.5")) should do

Decimal TryParse in VBA

亡梦爱人 提交于 2019-12-02 08:57:30
I am attempting to tryparse using decimal; however, I keep getting an "Object Required" run-time error. I'm not certain what I'm doing wrong. I'm used to doing a tryparse in C#. This is VBA, so the language translation is not clicking just yet. Any help appreciated. Sub try() Dim val As Variant Dim res As Boolean res = Decimal.TryParse("2.5", val) MsgBox (res & ":" & val) End Sub You can try CInt and check for a specific error using On Error Goto. res = cBool(Val("2.5")) should do the trick here, since any value <> 0 will evaluate as True 来源: https://stackoverflow.com/questions/23655251

C# While TryParse

早过忘川 提交于 2019-12-02 07:20:39
问题 char typeClient = ' '; bool clientValide = false; while (!clientValide) { Console.WriteLine("\nEntrez le type d'employé (c ou g) : "); clientValide = char.TryParse(Console.ReadLine(), out typeClient); if (clientValide) typeClient = 'c'; } I'd like to make it so it doesn't exit the while unless the char is 'g' or 'c' help ! :) 回答1: string input; do { Console.WriteLine("Entrez le type d'employé (c ou g):"); input = Console.ReadLine(); } while (input != "c" && input != "g"); char typeClient =

Conversion of a UTC date/time string in C#

做~自己de王妃 提交于 2019-12-01 19:23:40
问题 I need to convert the string "Fri Sep 11 00:00:00 GMT+04:00 2020" into a DateTime object 11.09.2011 . When I use DateTime result; DateTime.TryParseExact(MyString, "ddd MMM dd HH:mm:ss 'GMT'zzz yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out result); result is equal to {9/10/2020 11:00:00 PM} . How can I modify the code so that the date component is 11.09.2011 and not 10.09.2011 (I only need the date and don't care about the time) ? 回答1: Parse into a DateTimeOffset instead, given

Conversion of a UTC date/time string in C#

可紊 提交于 2019-12-01 17:58:14
I need to convert the string "Fri Sep 11 00:00:00 GMT+04:00 2020" into a DateTime object 11.09.2011 . When I use DateTime result; DateTime.TryParseExact(MyString, "ddd MMM dd HH:mm:ss 'GMT'zzz yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out result); result is equal to {9/10/2020 11:00:00 PM} . How can I modify the code so that the date component is 11.09.2011 and not 10.09.2011 (I only need the date and don't care about the time) ? Parse into a DateTimeOffset instead, given that you've got an offset. From the documentation of the zz specifier that you're using: With DateTime

How to (try)parse a single String to DateTime in “DD/MM/YYYY” format? (VB.Net)

人走茶凉 提交于 2019-12-01 08:38:48
How to (try)parse a single String to DateTime in "DD/MM/YYYY" format? (VB.Net) For example: I use input string "30/12/1999" (30 December 1999), how to (try)parse it to DateTime? Try this: Dim date As Datetime = DateTime.ParseExact(_ yourString, "dd/MM/yyyy", CultureInfo.InvariantCulture) This will throw an exception if yourString does not match the format you specify. If you do not want an exception in this case then do this: Dim date As Date Date.TryParseExact(dateString, "dd/MM/yyyy", CultureInfo.CurrentCulture, _ DateTimeStyles.None, date) 来源: https://stackoverflow.com/questions/2794305/how