tryparse

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

随声附和 提交于 2019-11-27 11:50:45
问题 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 please tell me why ? and how can I convert that string to date. EDIT: I just saw everybody mentioned to use format argument. I will mention now that I can't use the format parameter as I have some setting to select the custom dateformat what user wants,

UInt32.TryParse() hex-number not working

♀尐吖头ヾ 提交于 2019-11-27 03:53:51
问题 For some reason the following C# Console program always outputs: 32 False wtf=0 What am I doing wrong? using System.Collections.Generic; using System.Linq; using System.Text; using System.Globalization; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine(Convert.ToUInt32("0x20", 16)); UInt32 wtf = 0; Console.WriteLine(UInt32.TryParse("0x20", NumberStyles.HexNumber, // I've tried also AllowHexSpecifier CultureInfo.InvariantCulture, // I've also

Culture invariant Decimal.TryParse()

北城以北 提交于 2019-11-27 02:03:34
问题 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( string s, NumberStyles style, IFormatProvider provider, out decimal result ) I can't figure out what to use as the 3rd parameter. The examples I've seen look like this: culture = CultureInfo.CreateSpecificCulture("en-GB"); Decimal.TryParse(value, style

Integer.TryParse - a better way?

风格不统一 提交于 2019-11-27 00:53:01
问题 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. Usually it looks something like: Dim tempInt as Integer If Integer.TryParse(myInt, tempInt) Then I find this to be quite cumbersome considering that all I want is a simple True / False response. Is there a better way to approach this? Why isn't there an

DateTime.TryParse century control C#

天大地大妈咪最大 提交于 2019-11-26 23:03:18
The result of the following snippet is "12/06/1930 12:00:00". How do I control the implied century so that "12 Jun 30" becomes 2030 instead? string dateString = "12 Jun 30"; //from user input DateTime result; DateTime.TryParse(dateString, new System.Globalization.CultureInfo("en-GB"),System.Globalization.DateTimeStyles.None,out result); Console.WriteLine(result.ToString()); Please set aside, for the moment, the fact that a correct solution is to specify the date correctly in the first place. Note: The result is independant of the system datetime for the pc running the code. Answer: Thanks

Generic TryParse

你。 提交于 2019-11-26 11:08:51
I am trying to create a generic extension that uses 'TryParse' to check if a string is a given type: public static bool Is<T>(this string input) { T notUsed; return T.TryParse(input, out notUsed); } this won't compile as it cannot resolve symbol 'TryParse' As I understand, 'TryParse' is not part of any interface. Is this possible to do at all? Update: Using the answers below I have come up with: public static bool Is<T>(this string input) { try { TypeDescriptor.GetConverter(typeof(T)).ConvertFromString(input); } catch { return false; } return true; } It works quite well but I think using

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

老子叫甜甜 提交于 2019-11-26 10:07:34
问题 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 = int.Parse(Request.QueryString[\"id\"]); } catch { // deal with it } } DoSomethingSpectacularNow(id); It all seems a bit clunky and rubbish. How do you deal with your Request.QueryString[] s? 回答1: Below is an extension method that will allow you to write code like this: int id = request.QueryString.GetValue<int>("id"); DateTime

Generic TryParse

陌路散爱 提交于 2019-11-26 08:50:59
问题 I am trying to create a generic extension that uses \'TryParse\' to check if a string is a given type: public static bool Is<T>(this string input) { T notUsed; return T.TryParse(input, out notUsed); } this won\'t compile as it cannot resolve symbol \'TryParse\' As I understand, \'TryParse\' is not part of any interface. Is this possible to do at all? Update: Using the answers below I have come up with: public static bool Is<T>(this string input) { try { TypeDescriptor.GetConverter(typeof(T))

DateTime.TryParse century control C#

三世轮回 提交于 2019-11-26 08:34:46
问题 The result of the following snippet is \"12/06/1930 12:00:00\". How do I control the implied century so that \"12 Jun 30\" becomes 2030 instead? string dateString = \"12 Jun 30\"; //from user input DateTime result; DateTime.TryParse(dateString, new System.Globalization.CultureInfo(\"en-GB\"),System.Globalization.DateTimeStyles.None,out result); Console.WriteLine(result.ToString()); Please set aside, for the moment, the fact that a correct solution is to specify the date correctly in the first

Parse v. TryParse

戏子无情 提交于 2019-11-26 01:29:45
问题 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? 回答1: Parse throws an exception if it cannot parse the value, whereas TryParse returns a bool indicating whether it succeeded. TryParse does not just try / catch internally - the whole point of it is that it is implemented without exceptions so that it is fast. In