Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi folks,

 

I'm having trouble converting text to date and comparing them. Is there a similar to VB.Net's IsDate in C# 2005? Please check out the code below and see if you can find what's wrong. Yeah, I know, I'm a newbie... :) The code in red is where I'm getting the error messages:

 

 

--------------------------------------------------------------------------

 

1) Checking if the text of a TextBox is date:

 

if IsDate(this.Date1TextBox.Text)

{

 

}

 

--------------------------------------------------------------------------

 

2) Converting and comparing the dates of two TextBoxes:

 

if (Convert.ChangeType(this.Date1TextBox.Text, DateTime) > Convert.ChangeType(this.Date2TextBox.Text, DateTime))

{

 

}

 

--------------------------------------------------------------------------

 

Thanks a lot,

 

JC :)

Posted

1) Checking if the text of a TextBox is date:

 

if IsDate(this.Date1TextBox.Text)

{

 

}

One method for doing this is something along the lines of...

	public bool IsDate(object inValue)
	{
		bool bValid;

		try 
		{
			DateTime myDT = DateTime.Parse(inValue);
			bValid = true;
		}
		catch (FormatException e) 
		{
			bValid = false;
		}

		return bValid;
	}

2) Converting and comparing the dates of two TextBoxes:

 

if (Convert.ChangeType(this.Date1TextBox.Text, DateTime) > Convert.ChangeType(this.Date2TextBox.Text, DateTime))

{

 

}

 

That throws an error because the overload requires a type and your passing it an object. You could change the DateTime to typeof(DateTime), don't know if that would help though I haven't tried it.

Anybody looking for a graduate programmer (Midlands, England)?
  • Leaders
Posted (edited)

Mmmmm.... code....

/* TryParse tries to parse a string. If it succeeds, it stores
* the date in the OUT variable and returns true. If it fails, it
* returns false. 
*/
if(DateTime.TryParse(MyDateString, out MyDate)) {
// > and < operators are overloaded.
if(MyDate > DateTime.Now) {
	// It's the future!
}
}

 

P.S. Not to rag on you, Cags, but I would personally discourage using code like the code you posted:

	public bool IsDate(object inValue)
	{
		bool bValid;

		try 
		{
			DateTime myDT = DateTime.Parse(inValue);
			bValid = true;
		}
		catch (FormatException e) 
		{
			bValid = false;
		}

		return bValid;
	}

It essentially depends on exception handling for validation, and exception handling is specifically not intended to be used for program flow control, but rather for "exceptional circumstances" (and that's not exceptional in a good way, either).

Edited by snarfblam
[sIGPIC]e[/sIGPIC]
Posted
P.S. Not to rag on you' date=' Cags, but I would personally discourage using code like the code you posted:[/quote']

Not a problem I didn't like this method but it was the only one I could think of.

Anybody looking for a graduate programmer (Midlands, England)?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...