简体   繁体   English

C#程式当机

[英]C# program crash

Thanks guys for helping me to fix the 1 digit and >31 problems. 感谢大家帮助我解决1位数字和31个以上的问题。

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.Length <= 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! 我知道这有点混乱...但是它可以正常工作,并且是我的第一个C#应用程序:P再次感谢!

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 : 如果您要在日期未解析的情况下返回,则可以跳过所有验证代码,而只需使用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. 对于初学者,将代码包装在try catch块中。 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. 您正在执行TryParsing,然后验证大于31或小于1的整数,但在TryParse失败时不进行处理。

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. 如果dateString格式不正确,它将在ParseExact崩溃。
Use TryParseExact instead. 请改用TryParseExact

I would guess its your ParseExact crashing this...But I'm not totally sure... Wrap everything in a try catch or several try catches. 我猜你的ParseExact崩溃了……但是我不太确定……将所有内容都包裹在一个try catch或几个try catch中。 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...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM