The question is as follows. There are 2 classes foo
and Form1
( foo
is created during the work of Form1
). The Form1
form has a RichTextBox
in which the event log is displayed. If Ixeaschen occurs ( Exception e
) in the foo
class, you must transfer the e.Message
to the RichTextBox
in some way. How to realize this opportunity?
|
2 answers
Pass an instance of the Form1
class to the foo
constructor and save it in the private field of the foo
class. Now we can access the RichTextBox of the particular Form1
instance in which the foo
instance was created.
public class foo { private Form1 form; public foo(Form1 form) { this.form = form; } public someMethod() { try { // что-то делаем } catch (Exception e) { this.form.RichTextBox.Text += e.Message; } } }
In Form1
class:
foo f = new foo(this); f.someMethod();
- and if foo needs to be moved to wpf, you will have to redo foo. It is better to
try catch
inform1
, wheresomeMethod
is called and outpute.Message
inrichtextbox
in the samee.Message
. - Stack
|
The answer above is not true. Make an exception wrapper over a method of the foo class, which is called in the Form1 class. Passing client logic down the class hierarchy is bad.
- 2If we talk about architecture, the operations that make the exceptions obviously belong to the business logic, and should be uploaded to the appropriate layer. In addition, it is impossible to show the exception text to the user, this is purely debugging information, non-localized, and potentially violating data confidentiality. - VladD
|
form1.richTextBox.Text += e.Message;
- fori1ton