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
...
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...
What is the safe method to access an array element, without throwing IndexOutOfRangeException, something like TryParse, TryRead, using extension methods or LINQ?
...
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...
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?
...
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...
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...
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!
...
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...
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...
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
...
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...
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 ...
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...
Given a variant, does VBScript have an equivalent of C#'s DateTime.TryParse method?
...
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,...
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...
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)
For example: I use input string "30/12/1999" (30 December 1999), how to (try)parse it to DateTime?
...
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...