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?

  • and what's the problem? About try-catch heard something? - DreamChild
  • I heard. The question is not how to catch the exept. and how to transfer the info from the foo class to the Form1 class to bring this case to the RichTextBox. - naym
  • one
    somehow vaguely you described the problem. But anyway, the issue of transferring data from one class to another is not a difficult task - DreamChild
  • form1.richTextBox.Text += e.Message; - fori1ton
  • @ fori1ton form1.richTextBox is not accessible from foo without creating an instance of the class. And when creating an instance, the output will not be made to what the user directly sees. - naym

2 answers 2

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 in form1 , where someMethod is called and output e.Message in richtextbox in the same e.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.

  • 2
    If 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