Thanks guys for helping me to fix the 1 digit and >31 problems.
Now, one last thing... if a textbox has non-numeric characters or no characters at all, the program will crash.
here's the whole code:
private void generate_Click(object sender, EventArgs e)
{
int val = 0;
if (Int32.TryParse(dd.Text, out val))
{
if (val > 31) return;
else if (dd.Text.Length <= 1)
return;
}
if (Int32.TryParse(MM.Text, out val))
{
if (val > 31) return;
else if (MM.Text.Length <= 1)
return;
}
if (Int32.TryParse(hh.Text, out val))
{
if (val > 31) return;
else if (hh.Text.Leng开发者_开发知识库th <= 1)
return;
}
if (Int32.TryParse(M.Text, out val))
{
if (val > 31) return;
else if (M.Text.Length <= 1)
return;
}
if (Int32.TryParse(ss.Text, out val))
{
if (val > 31) return;
else if (ss.Text.Length <= 1)
return;
}
String dateString = yyyy.Text + dd.Text + MM.Text + hh.Text + M.Text + ss.Text;
DateTime timestamp = DateTime.ParseExact(dateString, "yyyyddMMhhmmss", CultureInfo.CurrentCulture);
long ticks = timestamp.Ticks;
long microseconds = ticks / 10;
convertedText.Text = microseconds.ToString("X");
}
What shall I put? I know it's a bit messy... but it works and it's my first C# application :P Thanks again!
If you are just going to return if the date does not parse, then you can skip all of the validation code and just use TryParseExact
:
private void generate_Click(object sender, EventArgs e)
{
String dateString = yyyy.Text + dd.Text + MM.Text + hh.Text + M.Text + ss.Text;
DateTime timestamp
if (!DateTime.TryParseExact(dateString, "yyyyddMMhhmmss", null,
DateTimeStyles.None, out timestamp ))
return;
long ticks = timestamp.Ticks;
long microseconds = ticks / 10;
convertedText.Text = microseconds.ToString("X");
}
For starters, wrap your code in a try catch block. It will prevent the exception from automatically crashing the program. You can catch the exception and notify the user there is a problem with the input.
Then you can do something like:
catch
{
MessageBox.Show("Please enter numbers only.");
}
Really in a production app, you'd want to do a little more like catch a specific type of error. But for starting out this will do just fine.
Will this solve your particular problem? you're doing TryParsing and then validating the integer with greater than 31 or less than 1 but not handling if the TryParse fails.
private void generate_Click(object sender, EventArgs e)
{
int val = 0;
if (Int32.TryParse(dd.Text, out val))
{
if (val > 31) return;
else if (dd.Text.Length <= 1)
return;
}
else
{
return;
}
if (Int32.TryParse(MM.Text, out val))
{
if (val > 31) return;
else if (MM.Text.Length <= 1)
return;
}
else
{
return;
}
if (Int32.TryParse(hh.Text, out val))
{
if (val > 31) return;
else if (hh.Text.Length <= 1)
return;
}
else
{
return;
}
if (Int32.TryParse(M.Text, out val))
{
if (val > 31) return;
else if (M.Text.Length <= 1)
return;
}
else
{
return;
}
if (Int32.TryParse(ss.Text, out val))
{
if (val > 31) return;
else if (ss.Text.Length <= 1)
return;
}
else
{
return;
}
String dateString = yyyy.Text + dd.Text + MM.Text + hh.Text + M.Text + ss.Text;
DateTime timestamp = DateTime.ParseExact(dateString, "yyyyddMMhhmmss", CultureInfo.CurrentCulture);
long ticks = timestamp.Ticks;
long microseconds = ticks / 10;
convertedText.Text = microseconds.ToString("X");
}
It would crash on ParseExact
if dateString
is not in the correct format.
Use TryParseExact
instead.
I would guess its your ParseExact
crashing this...But I'm not totally sure... Wrap everything in a try catch or several try catches. Then you can see what's going on and correct it...
Something like:
try{
//Parse something...
}catch(Exception ex)
{
throw ex; //I usually put a break point here when I'm debugging...
}
精彩评论