-
How to display a message
Hi All,
Here is my problem :
I use C#.net to write a class library project. In the class, i need to display some message on the screen, say, some warning or error message. However, in a class library, there is no System.Windows namespace. So how can i do that ? i'd like to have such func like messagebox, i.e, not only display message but also allow user to make choice, in a class library project, is that possible ?
Thanks in advance
-
post your code...my assumption would be you would return a string from your library and then that string would be displayed in a messagebox from your application.
-
Thank you for your reply, Memnoch1207
As an simplified example, the code logic is like following:
if ( ErrorFlag ) {
x = Messagebox (" Ignore the Error ? ", YesNo) ; // This is part i want
if ( x== NO )
return ;
}
So, i really need to display and deal with the message inside of the class, can't return it to application and let the application to deal with it. Any suggestion ?
-
If you really want to use a messagebox, simply add a reference to the Windows.Forms assembly. <opinion>However, I think the correct way to go about this would be to throw an error so that the application programmer can decided what course to take -- to silently continue, ask the user, or abort.</opinion>
-
sunburnt is right. It is poor design if a class library does any user interaction that the library user didn't explicitly ask for. Log whatever ignorable messages/warnings you have in some kind of log file and let the real errors throw exceptions.
-
Yes, i agree with you guys in general speaking. However, in my case, it is not good enough for the func to throw an exceptions. It has to finish the work based on the user's selection when error occurs. I know i can break the func into multi-funcs. But i really want to keep it into one piece, easy to use and maintain...
Anyway, the way i am doing now is to define a form and use the form in the class.
-
I still disagree with displaying messages in your component. Why not create an enum and pass this up the chain.
-
Or an error delegate. When the error occurs, the delegate gets called. It must be set to a app-supplied function, which can then ask the user for input or decide somehow else what to do, or it can cancel the process completly.