C# InvalidOperationException

发布于 2024-08-16 23:51:57 字数 386 浏览 2 评论 0原文

Nodo N;  

foreach (string S in listBox_nodos.Items)  
{  
    N = graph.getNodoName(S);  
    string comp = (string) listBox_nodos.SelectedItem;  
    if (comp == S)  
        System.Console.WriteLine(N.NAME);  
}  

我收到 InvalidOperationException 并且应用程序崩溃了。
不明白为什么。有什么帮助吗?谢谢。

编辑:“N.NAME”打印!然后崩溃了。
EDIT2:我试图捕获异常,应用程序无论如何都会崩溃。

Nodo N;  

foreach (string S in listBox_nodos.Items)  
{  
    N = graph.getNodoName(S);  
    string comp = (string) listBox_nodos.SelectedItem;  
    if (comp == S)  
        System.Console.WriteLine(N.NAME);  
}  

I get InvalidOperationException and the application crashes.
Can't get why. Any help? Thanks.

EDIT: The 'N.NAME' prints! And then goes the crash.
EDIT2: I've tried to catch the Exception, the application crashes anyway.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水溶 2024-08-23 23:51:57

您在哪一行收到异常?在不知道 listBox_nodos 具有什么数据类型的情况下,我的第一个猜测是 Items 不包含大量 string,而是 ListBoxItem

foreach (var item in listBox_nodos.Items)  
{  
    N = graph.getNodoName(item.Value); // or .SomethingElse

At which line do you get the exception? Without knowing what datatype listBox_nodos has, my first guess w2ould be that Items does not contain a lot of strings, but rather ListBoxItems

foreach (var item in listBox_nodos.Items)  
{  
    N = graph.getNodoName(item.Value); // or .SomethingElse
轻拂→两袖风尘 2024-08-23 23:51:57
  • 如前所述,完整的异常会很高兴看到(如果在 VS 调试器中,您可以从异常帮助器对话框或 Debug -> Windows -> Locals 中的 $exception 条目获取它)。最坏的情况你应该能够尝试 { .. } catch (Exception ex) { System.Console.WriteLine(ex); (异常的 ToString 包括堆栈跟踪)

    • 如果您不确定所涉及的类型,可以将 Items/SelectedItem 作为 IEnumerable/object 获取,然后对其进行 gettype() 并显示。
  • 当尝试显示内容时,可能还想尝试 MessageBox.Show,以防万一由于某种原因使用 Console 时出现异常。

  • 与异常无关,但看起来您正在对列表框项目进行交互以查找所选项目 - 如果这是真的,为什么不直接使用所选项目?

  • as mentioned before, the full exception would be nice to see (if in the VS debugger, you can get it from the exception helper dialog or the $exception entry in Debug -> Windows -> Locals). Worst-case you should be able to try { .. } catch (Exception ex) { System.Console.WriteLine(ex); (Exception's ToString includes the stack trace)

    • if you're not sure of the types involved, could get the Items/SelectedItem as IEnumerable/object and then gettype() on them and display that.
  • when trying to display things, may also want to try MessageBox.Show just in case the exception is coming from the use of Console for some reason.

  • unrelated to the exception, but it looks like you're interating over the listbox items to find the selected item - if that's true, why not just use the selected item directly?

稀香 2024-08-23 23:51:57

我以前也遇到过类似的问题,也与所选项目有关。我对其进行了广泛的调查,但未能找到解决方法。我最终重新制作了表单(尽管我认为只有控件是必要的),这似乎解决了它。

I have had a similar problem before, also related to selected items. I investigated it extensively and was not able to find a fix. I eventually wound up remaking the form (though I think just the control was necessary) and that seemed to fix it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文