kriswinner
Member
- Joined
- Apr 23, 2009
- Messages
- 23
- Programming Experience
- 10+
Some background:
I'm creating an application that uses several "multi-leveled" structures (structures within structures within structures....) to store reference data.
If I were to code as follows:
This would change the value of Controller.Modules(1).subelement1 to "test" because the value of TestModule is passed by reference instead of value (not sure why this would need to be that way, but it is what it is...).
Doing some research on how to get around this, I found many references on Copying or Cloning structures.
This is the reference that seems like it does what I need.
http://www.vbdotnetforums.com/vb-net-general-discussion/5802-simple-generalized-file-i-o-deep-copy-cloning-any-object.html
As shown in the supplied code, I added <Serializable()> as a prefix to my structures. When I call this function:
I get a "SerializationException was unhandled" (Type 'System.Xml.Linq.XCData' in Assembly 'System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.) runtime error message on the BinFormatter.Serialize line.
Three questions:
1) Is the "CloneObject" function solution able to be used in my application?
2) If it is, what is the fix for the XCData typed objects?
3) If not, is there a better way?
Thanks in advance for your help. - KW
I'm creating an application that uses several "multi-leveled" structures (structures within structures within structures....) to store reference data.
VB.NET:
<Serializable()> Public Structure ControllerType
Public Attributes As ControllerAttributeType
Public Description As XCData
Public RedundancyInfo As RedundancyInfoType
Public Security As SecurityType
Public SafetyInfo As SafetyInfoType
Public DataTypes() As GCCSDataType
Public Modules() As Modules_Type
End Structure
If I were to code as follows:
VB.NET:
Dim Controller as ControllerType
Dim TestModule as Modules_Type
TestModule = Controller.Modules(1)
TestModule.subelement1 = "test" 'subelement1 is part of Modules_Type (not shown)
This would change the value of Controller.Modules(1).subelement1 to "test" because the value of TestModule is passed by reference instead of value (not sure why this would need to be that way, but it is what it is...).
Doing some research on how to get around this, I found many references on Copying or Cloning structures.
This is the reference that seems like it does what I need.
http://www.vbdotnetforums.com/vb-net-general-discussion/5802-simple-generalized-file-i-o-deep-copy-cloning-any-object.html
As shown in the supplied code, I added <Serializable()> as a prefix to my structures. When I call this function:
VB.NET:
Public Function CloneObject(ByVal originalObject) As Object
Dim BinFormatter As New BinaryFormatter
Dim memStream As New MemoryStream
BinFormatter.Serialize(memStream, originalObject)
memStream.Position = 0
CloneObject = BinFormatter.Deserialize(memStream)
End Function
Three questions:
1) Is the "CloneObject" function solution able to be used in my application?
2) If it is, what is the fix for the XCData typed objects?
3) If not, is there a better way?
Thanks in advance for your help. - KW