Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1103017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:16:35+00:00 2026-05-17T01:16:35+00:00

PROBLEM: I have a Child class which uses DataContractSerialization and raises a Changed event

  • 0

PROBLEM:

I have a Child class which uses DataContractSerialization and raises a Changed event when its Name property is set.

<DataContract()>
Public Class Child

    Public Event Changed()

    <DataMember()>
    Private _Name As String

    Public Sub New(ByVal NewName As String)
        _Name = NewName
    End Sub

    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
            RaiseEvent Changed()
        End Set
    End Property

End Class

It is contained within a Parent class which also uses DataContractSerialization and handles the Changed event of the Child.

<DataContract()>
Public Class Parent

    <DataMember()>
    Private WithEvents Child As Child

    Private Sub Child_Changed() Handles Child.Changed

        'Handle changed event here...

    End Sub

End Class

The Parent class is serialzed and deserialized and all the data (including the Child) is saved and resored as expected.

However, after deserialization, the Changed event is never raised!

QUESTIONS:

I know the deserialization process bypasses class constructors, but shouldn’t the event be initialized?

Am I doing something wrong?

Is it possible to serialize/deserialize the Event?

Is there a better workaround than the following (see below)?

Is there a way to initialize the event in the OnDeserialzed method of the Child rather than the Parent (see below)?

WORKAROUND:

(1) Add a constructor to the Child class which takes an instance of itself as an argument.

(2) Add an OnDeserialized method to the Parent class which creates a New instance of the Child class based on the deserialzed instance of the Child class.

<OnDeserialized()>
Private Sub OnDeserializedMethod(ByVal Context As StreamingContext)

    Child = New Child(Child)

End Sub

Now the Changed event is raised as expected.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:16:36+00:00Added an answer on May 17, 2026 at 1:16 am

    The problem is not that the Changed event isn’t being fired; as long as the same class definition (with the setter that raises the event) is used for the deserialized object (with DataContract serialization that isn’t a requisite), the event will be raised. What’s happening is that the deserialized object no longer has the handler attached.

    You cannot serialize or deserialize event handlers; at the very least, you shouldn’t. Because they may point to references other than the current object reference, and because the deserialized object is a new reference in what is probably a different runtime, event handler references from the serialized object are useless on deserialization, because the reference will no longer point to the expected object in the new runtime’s heap.

    The solution is a little easier than your workaround, but based on the same idea; implement custom deserialization behavior in the parent that reattaches the handler to the child’s event:

    <OnDeserialized()>
    Private Sub OnDeserializedMethod(ByVal Context As StreamingContext)
    
        AddHandler Child.Changed AddressOf Me.Child_Changed
    
    End Sub
    

    This avoids the memory and time costs of instantiating a new Child just to destroy another one, and should do the same trick. It is technically possible to do this on the Child, but the Child would require knowledge of its Parent using a backreference. It’s also a little backwards; generally speaking, event raisers don’t “grab” event handlers, but instead are given them by other objects that contain or know about the handlers and the event.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem. It looks like ARC synchronizes my property with child class.
I have a little problem understanding how to create a child class's contructor, which
I have an abstract parent class which child classes that inherit from it. I
I have a subclass named child which inherits a class named parent. I am
I'm running into a problem where, I have a Window that contains a child
Problem: Have made a small mail program which works perfectly on my developer pc
Problem: I have a value that is set in $record, for instance 1.69. Then
I have an object of class Level that has several subsystems (child objects) it
I have a C++ class representing a hierarchically organised data tree which is very
I have a legacy DB which uses a guid to map children to the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.