How to NOT serialize a Property of a Class

G

Guest

I have a class that has some fields and properties that I want to serialize.
However, one of the fields is of type System.Web.UI.Webcontrols.Webcontrol
and it will not serialize so I want to mark it as "NonSerialized". However,
this property is not allowed on a property of this type. How can I designate
this property so that I can serialize the rest of the call without
serializing this property???

Thanks!

Property myControl() As System.Web.UI.WebControls.WebControl
Get
Return _myControl
End Get
Set(ByVal Value As System.Web.UI.WebControls.WebControl)
_myControl = Value
End Set
End Property
 
M

Martin Dechev

Hi,

The first thing that comes to mind is using two separate methods for
getting/seting (eg: Get_myControl() and Set_myControl(Value As WebControl))
the field (_myControl) instead of the property (myControl()). Then just mark
the field _myControl with the NonSerializedAttribute.

Hope this helps
Martin
 
B

BluDog

<System.Xml.Serialization.XmlIgnore()> _
Property myControl() As System.Web.UI.WebControls.WebControl
Get
Return _myControl
End Get
Set(ByVal Value As System.Web.UI.WebControls.WebControl)
_myControl = Value
End Set
End Property


Hope this helps

Blu.
 
G

Guest

Thank you Both ForYour answers. I used the combination of your answers to
solve my issue and it works great! Here is what my class ends up looking like


<Serializable()> _
Class TabMenuItem
....
<NonSerialized()> Private _RenderControl As
System.Web.UI.WebControls.WebControl

....

<System.Xml.Serialization.XmlIgnore()> _
Property RenderControl() As System.Web.UI.WebControls.WebControl
Get
If Not _RenderControl Is Nothing Then
Return _RenderControl
Else
_RenderControl =
System.Activator.CreateInstance(ControlType)
_RenderControl.ID = ControlId
Return _RenderControl
End If
End Get
Set(ByVal Value As System.Web.UI.WebControls.WebControl)
_RenderControl = Value
ControlType = Value.GetType
ControlId = Value.ID
End Set
End Property
....

End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top