|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Cloning a derived controlSerialization, but that is not working because Panel is not Serializable. I have also tried putting the object into a one member array, then using Array.Copy to copy the item to multiple members of a second array, but I cannot get that to work either. Each copied member of the new array has the same handle as the original object. Is there an alternate way to clone an object without creating additional copies as New? I have overloaded New, but the constructor is a long routine, and I would like to make multiple identical copies to display on the form. I think the code would run much faster if I could clone the object, rather than construct it multiple times. My code: Imports System.Runtime.Serialization.Formatters.Binary Imports System.Runtime.Serialization Imports System.IO <System.Serializable()> _ Public Class ControlGroup Inherits Panel Public Shared Function Clone(ByVal ControlGroup _ As ControlGroup) As ControlGroup Dim MemStream As New MemoryStream(5000) Dim BiFormatter As New BinaryFormatter(Nothing, _ New StreamingContext(StreamingContextStates.Clone)) (this line fails)>>> BiFormatter.Serialize(MemStream, ControlGroup) MemStream.Seek(0, SeekOrigin.Begin) Dim CG2 As ControlGroup = _ BiFormatter.Deserialize(MemStream) MemStream.Close() Return CG2 End Function The line indicated fails because the ControlGroup object inherits from Panel, and Panel is not serializable. The code was adapted from: http://www.freevbcode.com/ShowCode.asp?ID=5552 You can add a "Clone" method or property to your usercontrol then pass it a
new instance of your control. In the Clone method/property, you can assign the properties (assuming they are value types) to the new control properties. Show quoteHide quote "Charlie" wrote: > I want to clone an object that is inherited from Panel. I am using > Serialization, but that is not working because Panel is not Serializable. > > I have also tried putting the object into a one member array, then using > Array.Copy to copy the item to multiple members of a second array, but I > cannot get that to work either. Each copied member of the new array has the > same handle as the original object. > > Is there an alternate way to clone an object without creating additional > copies as New? > > I have overloaded New, but the constructor is a long routine, and I would > like to make multiple identical copies to display on the form. I think the > code would run much faster if I could clone the object, rather than construct > it multiple times. > > My code: > > Imports System.Runtime.Serialization.Formatters.Binary > Imports System.Runtime.Serialization > Imports System.IO > <System.Serializable()> _ > Public Class ControlGroup > Inherits Panel > > Public Shared Function Clone(ByVal ControlGroup _ > As ControlGroup) As ControlGroup > Dim MemStream As New MemoryStream(5000) > Dim BiFormatter As New BinaryFormatter(Nothing, _ > New StreamingContext(StreamingContextStates.Clone)) > (this line fails)>>> BiFormatter.Serialize(MemStream, ControlGroup) > MemStream.Seek(0, SeekOrigin.Begin) > Dim CG2 As ControlGroup = _ > BiFormatter.Deserialize(MemStream) > MemStream.Close() > Return CG2 > End Function > > The line indicated fails because the ControlGroup object inherits from > Panel, and Panel is not serializable. > > The code was adapted from: > http://www.freevbcode.com/ShowCode.asp?ID=5552 > > > |
|||||||||||||||||||||||