Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The likely failure mode here is that your class has an event that was subscribed. And the target of the event handler was a method that's part of a Form class. Yes, that cannot work, the Form class is not serializable. Nor would you <em>want</em> to serialize the event, it is pretty unlikely to deserialize properly.</p> <p>You suppress serialization of a member of a class by applying the <code>&lt;NonSerialized&gt;</code> attribute on the member. Unfortunately that's pretty hard to do in VB.NET, it does not work on a member declared with the Event keyword. VB.NET events are rather special. The workaround is to use a Custom Event instead, one that use a private backing field which you can prevent from getting serialized. That looks like this:</p> <pre><code>&lt;Serializable()&gt; _ Class ClassWithEvents &lt;NonSerialized()&gt; _ Private Events As New System.ComponentModel.EventHandlerList Public Custom Event Foo As EventHandler AddHandler(ByVal value As EventHandler) Events.AddHandler("foo", value) End AddHandler RemoveHandler(ByVal value As EventHandler) Events.RemoveHandler("foo", value) End RemoveHandler RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs) CType(Events("foo"), EventHandler).Invoke(sender, e) End RaiseEvent End Event '' etc.. End Class </code></pre> <p>You'll have to do that for every event in the class. You can re-use the Events variable, just change "foo" to "bar". Not a lot of fun of course. Do consider a dedicated class that only has the properties you want to serialize as an alternative.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload