What it means: Loaded + = MyWindow_Loaded;

I have a vb.net background and am trying to learn the C # syntax

+2


source to share


4 answers


This means that you are subscribing to the event Loaded

and the event listener is a method MyWindow_Loaded

in your class.



+2


source


This is the same as saying:

AddHandler Me.Loaded, AddressOf(MyWindow_Loaded)

      

Although in your code it can be part of a method declaration like this:



Private Sub MyWindowLoaded(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Loaded
    ...
End Sub

      

In any case, this means that you are attaching a function named MyWindow_Loaded

to the event Loaded

.

+11


source


While you can easily figure out what it is supposed to do, the syntax can be a little confusing. Making a padding between an event and a method doesn't really make sense, but the effect is of course related to the event handler.

The + = operator for an event attaches an event handler to the event. However, there is a shortcut that the compiler makes here to convert the method to a delegate. In fact, the generated code (which you can also use) looks something like this:

Loaded += new LoadedEventHandler(MyWindow_Loaded);

      

So what the code does is create an event handler delegate for the method and bind it to the event. The delegate type is determined by the event expected.

+3


source


It attaches a method MyWindow_Loaded

to the event Loaded

. Please see this for more information.

+2


source







All Articles