tryparse

Enum.TryParse returns true for any numeric values

别来无恙 提交于 2019-12-28 11:50:07
问题 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 = Enum.TryParse("1234", out outputEnum); Despite the string "1234" not being a possible value, result will come back as true, and my outputEnum will have a value of 1234. Is there a way I can avoid this sort of behavior? I'm trying to write a function

Enum.TryParse returns true for any numeric values

吃可爱长大的小学妹 提交于 2019-12-28 11:49:32
问题 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 = Enum.TryParse("1234", out outputEnum); Despite the string "1234" not being a possible value, result will come back as true, and my outputEnum will have a value of 1234. Is there a way I can avoid this sort of behavior? I'm trying to write a function

What should the out value be set to with an unsuccessfull TryXX() method?

谁说胖子不能爱 提交于 2019-12-24 01:44:26
问题 I'm implementing a TryParse(string s, Out object result) method. If the parse fails, I would like not to touch the out parameter so any previous result will remain intact. But VS2k8 won't let me. I have to set the value of the out object no matter what. Should I just put result = result for the sake of pleasing the compiler? Am I missing something? 回答1: Your suggestion of result = result won't work, because it's an out parameter - it's not definitely assigned to start with, so you can't read

Does VBScript have a DateTime.TryParse equivalent?

淺唱寂寞╮ 提交于 2019-12-23 10:28:27
问题 Given a variant, does VBScript have an equivalent of C#'s DateTime.TryParse method? 回答1: Use IsDate(stringDate) in conjunction with CDate(stringDate). Use the IsDate() function to determine if date can be converted to a date or time. CDate() recognizes date literals and time literals as well as some numbers that fall within the range of acceptable dates. When converting a number to a date, the whole number portion is converted to a date. Any fractional part of the number is converted to a

Convert a string in a List<int> using LINQ (cleaner way)

梦想的初衷 提交于 2019-12-23 02:39:26
问题 I have this string: string input = "1,2,3,4,s,6"; Pay attention to the s character. I just want to convert this string in a List<int> using LINQ. I initially tried in this way: var myList = new List<int>(); input.Split(',').ToList().ForEach(n => myList.Add(int.TryParse(n, out int num) ? num : -1) ); lista.RemoveAll(e => e == -1); But I prefer not have any -1 instead of a no-number characters. So now I try with this: var myList = new List<int>(); input.Split(',').ToList() .FindAll(n => int

int.TryParse = null if not numeric?

只愿长相守 提交于 2019-12-21 07:15:37
问题 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 回答1: 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

Int.TryParse() returns false always

北战南征 提交于 2019-12-19 07:36:07
问题 I have following code int varOut; int.TryParse(txt1.Text, out varOut); // Here txt1.Text = 4286656181793660 Here txt1.Text is the random 16 digit number generated by JavaScript which is an integer. But the above code always return false i.e. varOut value is always zero. What I am doing wrong here ? 回答1: The limit for int ( 32-bit integer) is -2,147,483,648 to 2,147,483,647 . Your number is too large. For large integer number such as your case, try to Parse using long.TryParse (or Int64

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

眉间皱痕 提交于 2019-12-17 23:14:28
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . 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... } 回答1: Better is highly subjective. For instance, I

string to double skips last decimal if it is zero?

廉价感情. 提交于 2019-12-17 21:17:10
问题 Here is my Problem. I need to Convert say "5.550" (string) to double as 5.550 that is double with 3 decimal digits. i have tried IFormatProvider while parsing but no use.it keeps skipping last zero(). Please advice. Thanks, Kumar M A 回答1: double doesn't keep insignificant digits - there's no difference between 1.5 and 1.50000 as far as double is concerned. If you want to preserve insignificant digits, use decimal instead. It may well be more appropriate for you anyway, depending on your exact

Parsing floats with Single.TryParse fails

淺唱寂寞╮ 提交于 2019-12-12 21:23:04
问题 There is an article on Single.TryParse over at MSDN with this example code: http://msdn.microsoft.com/en-us/library/26sxas5t%28v=vs.100%29.aspx // Parse a floating-point value with a thousands separator. value = "1,643.57"; if (Single.TryParse(value, out number)) Console.WriteLine(number); else Console.WriteLine("Unable to parse '{0}'.", value); Problem is in the article the TryParse returns true and the string is converted, but when I try it, it's false . How do I fix this? UPD: To simplify