English 中文(简体)
我如何用非古董会相关班子向全球评价系统显示错误信息?
原标题:How do I display an error message for a GUI app from non-GUI related classes?

我知道,我可以使用<代码>MessageBox.Show(“一些错误”),但我说的是,在我的法典中,在某种较低层次上出现了一种错误,即没有企业要放弃一种电文,或者没有其他电文。

I m building an RSS Client and I have a class which manages the various feeds (lets say FeedManager) which is just a wrapper for a list at this point. Now this class calls on another class for data access. So anytime someone in the GUI wants to save the feeds, the GUI simply calls FeedManager.SaveFeeds() which processes the feeds and saves them to a file, database, etc. So I attempt to save to a file and something bad happens (either something I coded for or an exception). So now I m at least 3 levels deep, GUI -> FeedManager -> SomeDataAccessLayer, and I want to display a message to the user like "Hey, that file was not found" or "You don t have permission to write to that location", etc...

我应如何做到这一点? 在数据存取层上安装了MessageBox,这明目张胆地表明,构成家庭调查的夫妇。 采用所有方法的回报都带有任何错误信息,似乎也令人难以置信。

最佳回答

非古阿姆集团的法典实际上不应显示一种电文。

标准做法是放弃例外。

您的全球倡议应围绕以下呼吁:SaveFiles(>,并有一个试捕组/副渔获物组,并采取适当的行动,如展示一个信息箱。

Maybe you overlooked the point is that this is exactly what Exceptions are for: to communicate errors over (multiple) method calls.

问题回答

也许你可以设立一个处理错误的新班级,这一班级(根据你的意愿)将这一班印到青厅,以公开静态通用公平观的组成部分等形式展示。 这样,你就能够轻松地把它从全球倡议中解脱出来,但仍在其中传达信息。

You should throw exception instead of message box

My solution for this was to use an event with a string parameter and whenever an exception (or any other error) occurred I triggered the event and past to it the message.

In the GUI class that create the instance just need to register to that event and in case it was triggered pop up a message box (or any other info).

namespace XXX {
  public delegate void Error(string a_sErrorMessage);

  public class XXX {

    public event Error OnError;

    public void Test() {

      try {
        // Do something
      } catch (Exception ex) {
        // Trigger the event
        OnError(ex.Message);
      }
    }
  }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签