C # 4: collecting and subscribing to events between static classes

I spent the last couple of days reading my different books and looking at the MSDN documentation, and I just can't get what seems like an extremely simple task to work with.

Here's what I want to do in a nutshell: I have a static DBToolBox class that performs various functions in SQL databases, and I want it to have an error reporting system that is different from the user interface. I want to use an event to signal when the log (DataTable) has been updated to update the new static class itself, a Windows form using a DataGridView. Here is the code I cannot work with:

Alarm Class:

public static class DBTools { public static readonly DataTable ErrorLog = new DataTable(); public static event EventHandler LogUpdated = delegate {}; // the actual functionality of the class private static void Error(Exception Ex, string MethodName) { ErrorLog.Rows.Add((); //logs the error with a bunch of data that I'm not listing here LogUpdated(null, EventArgs.Empty); //I attempt to raise an event } } 

Reactive class:

 public static partial class ErrorWindow : Form { DBToolbox.LogUpdated += ErrorWindow.ErrorResponse; \\the offending event handler: \\invalid token "+=" in class, struct, or interface member declaration \\invalid token ";" in class, struct, or interface member declaration \\'QueryHelper_2._0.DBToolbox.LogUpdated' is a 'field' but is used like a 'type' \\'QueryHelper_2._0.ErrorWindow.ErrorResponse(object)' is a 'method' but is used like a 'type' private void Error_Load(object sender, EventArgs e) { ErrorLogView.DataSource = DBToolbox.ErrorLog; } public void ErrorResponse(object sender) { this.Show(); this.ErrorLogView.DataSource = DBToolbox.ErrorLog; this.ErrorLogView.Refresh(); this.Refresh(); } } 

}

What am I doing wrong?

In addition, there are two other solutions that I can execute: Firstly, these are my own DataTable RowUpdated or NewTableRow events, but I'm not sure how to sign this event.

Another event is a DataGridVeiw DataSourceChanged, but I do not know if this means that the event fires when the DataSource address changes, if it changes values.

I also have been pursuing a career in C # for about a week and a half, but I programmed VB2010 about a year before that, so I'm not very familiar with the .NET 4 function library.

+4
source share
3 answers

First of all: partial classes are only static if all parts are declared as such. In addition (as far as I know, since I currently do not have the means to test it), static classes cannot be inherited either by themselves or inherit from another class. Last but not least, derived user interface elements always need instantiated classes, since all the basic elements are instance-based.

And in order to bind an event handler to an event, you must do this inside the method body (i.e. in the constructor):

 public ErrorWindow() { InitializeComponent(); // Needed to init Winforms stuff DBToolbox.LogUpdated += ErrorResponse; } 

In addition, you will need to modify the ErrorResponse event handler to match the signature of void EventHandler(object, EventArgs) .

+1
source

Line

 DBToolbox.LogUpdated += ErrorWindow.ErrorResponse; 

should be in the method. Try adding a static constructor to ErrorWindow, which includes this line.

 static ErrorWindow() { DBToolbox.LogUpdated += ErrorWindow.ErrorResponse; } 
+2
source

Try this.ErrorLogView.DataBind() after setting the DataSource.

0
source

Source: https://habr.com/ru/post/1394916/


All Articles