tryparse

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 = int.Parse(Request.QueryString["id"]); } catch { // deal with it ...

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. Usually it looks something like: Dim tempInt as Integer If Integer.TryParse(myInt, tempInt) T...

Safe element of array access

What is the safe method to access an array element, without throwing IndexOutOfRangeException, something like TryParse, TryRead, using extension methods or LINQ? ...

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

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 missin...

Why DateTime.TryParse returning false when given a real year string?

In the code below I am giving the function a sTransactionDate="1999" and I am trying to covert it to a date x/x/1999. DateTime dTransactionDate = new DateTime(); if(DateTime.TryParse(sTransactionDate, out dTransactionDate)) { //Happy }else { //Sad } if the string is "1999" it will always end up in sad. Any ideas? ...

Using TypeDescriptor in place of TryParse

Hi Guys I am trying to replicate TryParse for generic types and thought that TypeDescriptor might give me what I am after. So I came up with the following test case but it is failing, just wondering if anyone knows where I am going wrong. [TestMethod] public void Test() { string value = "Test"; Guid resultV...

Int32.TryParse() or (int?)command.ExecuteScalar()

I have a SQL query which returns only one field - an ID of type INT. And I have to use it as integer in C# code. Which way is faster and uses less memory? int id; if(Int32.TryParse(command.ExecuteScalar().ToString(), out id)) { // use id } or int? id = (int?)command.ExecuteScalar(); if(id.HasValue) { // use id.Value } or int...

VB.NET Double Question

Hello Currently I have a Double which looks like 12.53467345 .. Now I would like to remove the numbers after the dot so i just get "12" , how could i do this? I guess with TryParse, but don't really understand how to do it. Thanks! ...

MembershipUser.TryParse()

Anyone know an equiv? Currently I'm doing.. Dim myUsers As New MembershipUserCollection Dim myUser As MembershipUser Dim RoleUsers() As String RoleUsers = Roles.GetUsersInRole("User") For Each x As String In RoleUsers Dim roleUser As MembershipUser roleUser = Membership.GetUser(x) If Not roleUser Is Nothing Then m...

GUID.TryParse() ?

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 argument in the comments section about RegEx A doesn't work, use RegEx B. Then someone would write Regex C yadda yadda So anyway, What I decided to do w...

int.TryParse = null if not numeric?

Hello 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 ...

DateTime.TryParse century control C#

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...

Is this value a valid year C#

What's the best way to check if a string is a valid year using C#? I currently have a dropdown list that contains the values {'All','2009','2008'} etc, and I want to know whether the selection is one of the dates or the 'All' field. Currently I'm checking for bool isYearValid = (Year.ToLower() == "all") ? false : true; How do I check ...

pros and cons of TryCatch versus TryParse

What are the pros and cons of using either of the following approaches to pulling out a double from an object? Beyond just personal preferences, issues I'm looking for feedback on include ease of debugging, performance, maintainability etc. public static double GetDouble(object input, double defaultVal) { try { return Co...

Does VBScript have a DateTime.TryParse equivalent?

Given a variant, does VBScript have an equivalent of C#'s DateTime.TryParse method? ...

Enum.TryParse with Flags attribute

I have written code to TryParse enum either by value or by its name as shown below. How can I extend this code to include parsing enums with Flags attribute? public static bool TryParse<T>(this T enum_type, object value, out T result) where T : struct { return enum_type.TryParse<T>(value,...

Data Validation for Random varying Phone Numbers

I am storing phone numbers of varying lengths in my WPF (C#, VS 08) App. I store them as strings. My question is about my method AddNewPhoneNo(string phoneNo). In this method, I use Int.TryParse to validate the incoming number (i.e. not null, is numeric...). I've since realized this is probably not the best way to do this, because then...

Regex for date.

What should be the regex for matching date of any format like 26FEB2009 30 Jul 2009 27 Mar 2008 29/05/2008 27 Aug 2009 What should be the regular expression for that ? I have regex that matches with 26-Feb-2009 and 26 FEB 2009 with but not with 26FEB2009. So if any one know then please update it. (?:^|[^\d\w:])(?'day'\d{1,2}...

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

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? ...

UInt32.TryParse() hex-number not working

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) { Conso...