<html> <head> <META http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Object Clone</title> <link rel="stylesheet" type="text/css" href="../../../style.css"> </head> <body> <div class="CommonContent"> <div class="CommonContentArea"> <h1>Object Clone</h1><script> //We attach this function to a browser object as a variable so that if multiple of these blocks are //present on the same page, then they will simply over-write eachother with the same function instead //of generating a naming collision. window.WikiCodeFormattingCopyToClipboard = function copyToClipboard(sID){ var sContent = document.getElementById(sID).innerText; if( window.clipboardData && clipboardData.setData ) { clipboardData.setData("Text", sContent); } else { alert("You must enable javascript access to your clipboard for this feature to work. Please referr to your browser documentation or Google search for instructions."); } } </script><p>Platform implementations of #clone is not compatible with TP. </p><p>Both java and .NET object implementations provide <code>#clone</code> method for default objects, which is enabled by implementing <code>Cloneable/ICloneable</code> interface. This implementation is a shallow clone, i.e. only the top-level object fields are duplicated, all the referenced(children) objects are only copied as references to the same object in the parent clone. But how it affects Transparent Persistence?</p> <p>If you remember <a href="../../object_lifecycle/transparent_persistence/transparent_persistence_implementation.html" class="wikiLink">Transparent Persistence Implementation</a> you must know that a special <code>Activator</code> field is used to bind an object to the object container. Consequently, the default clone will copy this <code>Activatable</code> field to the object's duplicate, which will produce ambiguity as the object container won't know which object should be activated for write.</p> <p>Let's look how it will affect db4o in practice. We will use a usual <a href="car.html" class="wikiLink">Car</a> class and make it cloneable. Use the following code to store a car object and it's clone:<br></p> <span name="cs_wiki_filter" csw_filters="cs"> <div class="FormattedSourceCode"><div class="fscHeader"><span class="fscFileName">TPCloneExample.cs: <span class="fscMemberName">StoreCar</span></span></div><div class="fscCode"><pre ID="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53"><div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #008080;">01</span><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/None.gif" align="top"/><span style="color: #0000FF;">private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> StoreCar() </span><span style="color: #008080;">02</span><span style="color: #000000;"><img id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_39_637_Open_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_39_637_Open_Text').style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_39_637_Closed_Image').style.display='inline'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_39_637_Closed_Text').style.display='inline';"/><img id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_39_637_Closed_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_39_637_Closed_Text').style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_39_637_Open_Image').style.display='inline'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_39_637_Open_Text').style.display='inline';"/> </span><span id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_39_637_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">...</span><span id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_39_637_Open_Text"><span style="color: #000000;">{ </span><span style="color: #008080;">03</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> File.Delete(Db4oFileName); </span><span style="color: #008080;">04</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> IObjectContainer container </span><span style="color: #000000;">=</span><span style="color: #000000;"> Database(Db4oFactory.NewConfiguration()); </span><span style="color: #008080;">05</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (container </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">) </span><span style="color: #008080;">06</span><span style="color: #000000;"><img id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_210_627_Open_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_210_627_Open_Text').style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_210_627_Closed_Image').style.display='inline'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_210_627_Closed_Text').style.display='inline';"/><img id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_210_627_Closed_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedSubBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_210_627_Closed_Text').style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_210_627_Open_Image').style.display='inline'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_210_627_Open_Text').style.display='inline';"/> </span><span id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_210_627_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">...</span><span id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_210_627_Open_Text"><span style="color: #000000;">{ </span><span style="color: #008080;">07</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">try</span><span style="color: #000000;"> </span><span style="color: #008080;">08</span><span style="color: #000000;"><img id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_248_516_Open_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_248_516_Open_Text').style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_248_516_Closed_Image').style.display='inline'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_248_516_Closed_Text').style.display='inline';"/><img id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_248_516_Closed_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedSubBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_248_516_Closed_Text').style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_248_516_Open_Image').style.display='inline'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_248_516_Open_Text').style.display='inline';"/> </span><span id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_248_516_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">...</span><span id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_248_516_Open_Text"><span style="color: #000000;">{ </span><span style="color: #008080;">09</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #008000;">//</span><span style="color: #008000;"> create a car</span><span style="color: #008000;"> </span><span style="color: #008080;">10</span><span style="color: #008000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/></span><span style="color: #000000;"> Car car </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Car(</span><span style="color: #800000;">"</span><span style="color: #800000;">BMW</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Pilot(</span><span style="color: #800000;">"</span><span style="color: #800000;">Rubens Barrichello</span><span style="color: #800000;">"</span><span style="color: #000000;">)); </span><span style="color: #008080;">11</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> container.Store(car); </span><span style="color: #008080;">12</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> Car car1 </span><span style="color: #000000;">=</span><span style="color: #000000;"> (Car)car.Clone(); </span><span style="color: #008080;">13</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> container.Store(car1); </span><span style="color: #008080;">14</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"/> }</span></span><span style="color: #000000;"> </span><span style="color: #008080;">15</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">finally</span><span style="color: #000000;"> </span><span style="color: #008080;">16</span><span style="color: #000000;"><img id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_557_613_Open_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_557_613_Open_Text').style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_557_613_Closed_Image').style.display='inline'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_557_613_Closed_Text').style.display='inline';"/><img id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_557_613_Closed_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedSubBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_557_613_Closed_Text').style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_557_613_Open_Image').style.display='inline'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_557_613_Open_Text').style.display='inline';"/> </span><span id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_557_613_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">...</span><span id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_StoreCar_1182_53_557_613_Open_Text"><span style="color: #000000;">{ </span><span style="color: #008080;">17</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> CloseDatabase(); </span><span style="color: #008080;">18</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"/> }</span></span><span style="color: #000000;"> </span><span style="color: #008080;">19</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"/> }</span></span><span style="color: #000000;"> </span><span style="color: #008080;">20</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockEnd.gif" align="top"/> }</span></span></div></pre></div></div> </p> </span> <span name="cs_wiki_filter" csw_filters="vb"> <div class="FormattedSourceCode"><div class="fscHeader"><span class="fscFileName">TPCloneExample.vb: <span class="fscMemberName">StoreCar</span></span></div><div class="fscCode"><pre ID="..TPCloneVB.ZipsTPCloneExample.Vb_TPCloneExample.Vb_StoreCar_1305_53"><div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #008080;">01</span><img id="..TPCloneVB.ZipsTPCloneExample.Vb_TPCloneExample.Vb_StoreCar_1305_53_15_603_Open_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('..TPCloneVB.ZipsTPCloneExample.Vb_TPCloneExample.Vb_StoreCar_1305_53_15_603_Open_Text').style.display='none'; document.getElementById('..TPCloneVB.ZipsTPCloneExample.Vb_TPCloneExample.Vb_StoreCar_1305_53_15_603_Closed_Image').style.display='inline'; document.getElementById('..TPCloneVB.ZipsTPCloneExample.Vb_TPCloneExample.Vb_StoreCar_1305_53_15_603_Closed_Text').style.display='inline';"/><img id="..TPCloneVB.ZipsTPCloneExample.Vb_TPCloneExample.Vb_StoreCar_1305_53_15_603_Closed_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('..TPCloneVB.ZipsTPCloneExample.Vb_TPCloneExample.Vb_StoreCar_1305_53_15_603_Closed_Text').style.display='none'; document.getElementById('..TPCloneVB.ZipsTPCloneExample.Vb_TPCloneExample.Vb_StoreCar_1305_53_15_603_Open_Image').style.display='inline'; document.getElementById('..TPCloneVB.ZipsTPCloneExample.Vb_TPCloneExample.Vb_StoreCar_1305_53_15_603_Open_Text').style.display='inline';"/><span style="color: #0000FF;">Private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Shared</span><span style="color: #000000;"> </span><span id="..TPCloneVB.ZipsTPCloneExample.Vb_TPCloneExample.Vb_StoreCar_1305_53_15_603_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">Sub StoreCar()</span><span id="..TPCloneVB.ZipsTPCloneExample.Vb_TPCloneExample.Vb_StoreCar_1305_53_15_603_Open_Text"><span style="color: #0000FF;">Sub</span><span style="color: #000000;"> StoreCar() </span><span style="color: #008080;">02</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> File.Delete(Db4oFileName) </span><span style="color: #008080;">03</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> container </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> IObjectContainer </span><span style="color: #000000;">=</span><span style="color: #000000;"> Database(Db4oFactory.NewConfiguration()) </span><span style="color: #008080;">04</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">If</span><span style="color: #000000;"> container </span><span style="color: #0000FF;">IsNot</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Nothing</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Then</span><span style="color: #000000;"> </span><span style="color: #008080;">05</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Try</span><span style="color: #000000;"> </span><span style="color: #008080;">06</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #008000;">'</span><span style="color: #008000;"> create a car</span><span style="color: #008000;"> </span><span style="color: #008080;">07</span><span style="color: #008000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/></span><span style="color: #000000;"> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> car </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">New</span><span style="color: #000000;"> Car(</span><span style="color: #800000;">"</span><span style="color: #800000;">BMW</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #0000FF;">New</span><span style="color: #000000;"> Pilot(</span><span style="color: #800000;">"</span><span style="color: #800000;">Rubens Barrichello</span><span style="color: #800000;">"</span><span style="color: #000000;">)) </span><span style="color: #008080;">08</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> container.Store(car) </span><span style="color: #008080;">09</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> car1 </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> Car </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">DirectCast</span><span style="color: #000000;">(car.Clone(), Car) </span><span style="color: #008080;">10</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> container.Store(car1) </span><span style="color: #008080;">11</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Finally</span><span style="color: #000000;"> </span><span style="color: #008080;">12</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> CloseDatabase() </span><span style="color: #008080;">13</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">End</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Try</span><span style="color: #000000;"> </span><span style="color: #008080;">14</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">End</span><span style="color: #000000;"> </span><span style="color: #0000FF;">If</span><span style="color: #000000;"> </span><span style="color: #008080;">15</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockEnd.gif" align="top"/> </span><span style="color: #0000FF;">End Sub</span></span></div></pre></div></div> </p> </span> <p>So it works for the first store, but what if we will clone an object retrieved from the database?</p> <span name="cs_wiki_filter" csw_filters="cs"> <div class="FormattedSourceCode"><div class="fscHeader"><span class="fscFileName">TPCloneExample.cs: <span class="fscMemberName">TestClone</span></span></div><div class="fscCode"><pre ID="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58"><div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #008080;">01</span><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/None.gif" align="top"/><span style="color: #0000FF;">private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> TestClone() </span><span style="color: #008080;">02</span><span style="color: #000000;"><img id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_40_1021_Open_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_40_1021_Open_Text').style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_40_1021_Closed_Image').style.display='inline'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_40_1021_Closed_Text').style.display='inline';"/><img id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_40_1021_Closed_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_40_1021_Closed_Text').style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_40_1021_Open_Image').style.display='inline'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_40_1021_Open_Text').style.display='inline';"/> </span><span id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_40_1021_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">...</span><span id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_40_1021_Open_Text"><span style="color: #000000;">{ </span><span style="color: #008080;">03</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> IConfiguration configuration </span><span style="color: #000000;">=</span><span style="color: #000000;"> ConfigureTP(); </span><span style="color: #008080;">04</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #008080;">05</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> IObjectContainer container </span><span style="color: #000000;">=</span><span style="color: #000000;"> Database(configuration); </span><span style="color: #008080;">06</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (container </span><span style="color: #000000;">!=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">) </span><span style="color: #008080;">07</span><span style="color: #000000;"><img id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_214_1011_Open_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_214_1011_Open_Text').style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_214_1011_Closed_Image').style.display='inline'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_214_1011_Closed_Text').style.display='inline';"/><img id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_214_1011_Closed_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedSubBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_214_1011_Closed_Text').style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_214_1011_Open_Image').style.display='inline'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_214_1011_Open_Text').style.display='inline';"/> </span><span id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_214_1011_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">...</span><span id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_214_1011_Open_Text"><span style="color: #000000;">{ </span><span style="color: #008080;">08</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">try</span><span style="color: #000000;"> </span><span style="color: #008080;">09</span><span style="color: #000000;"><img id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_252_900_Open_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_252_900_Open_Text').style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_252_900_Closed_Image').style.display='inline'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_252_900_Closed_Text').style.display='inline';"/><img id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_252_900_Closed_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedSubBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_252_900_Closed_Text').style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_252_900_Open_Image').style.display='inline'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_252_900_Open_Text').style.display='inline';"/> </span><span id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_252_900_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">...</span><span id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_252_900_Open_Text"><span style="color: #000000;">{ </span><span style="color: #008080;">10</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> IObjectSet result </span><span style="color: #000000;">=</span><span style="color: #000000;"> container.QueryByExample(</span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Car(</span><span style="color: #0000FF;">null</span><span style="color: #000000;">, </span><span style="color: #0000FF;">null</span><span style="color: #000000;">)); </span><span style="color: #008080;">11</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> ListResult(result); </span><span style="color: #008080;">12</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> Car car </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">; </span><span style="color: #008080;">13</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> Car car1 </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">; </span><span style="color: #008080;">14</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (result.Size() </span><span style="color: #000000;">></span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">) </span><span style="color: #008080;">15</span><span style="color: #000000;"><img id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_517_882_Open_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_517_882_Open_Text').style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_517_882_Closed_Image').style.display='inline'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_517_882_Closed_Text').style.display='inline';"/><img id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_517_882_Closed_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedSubBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_517_882_Closed_Text').style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_517_882_Open_Image').style.display='inline'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_517_882_Open_Text').style.display='inline';"/> </span><span id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_517_882_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">...</span><span id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_517_882_Open_Text"><span style="color: #000000;">{ </span><span style="color: #008080;">16</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> car </span><span style="color: #000000;">=</span><span style="color: #000000;"> (Car)result[</span><span style="color: #800080;">0</span><span style="color: #000000;">]; </span><span style="color: #008080;">17</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> System.Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Retrieved car: </span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> car); </span><span style="color: #008080;">18</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> car1 </span><span style="color: #000000;">=</span><span style="color: #000000;"> (Car)car.Clone(); </span><span style="color: #008080;">19</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> System.Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Storing cloned car: </span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> car1); </span><span style="color: #008080;">20</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> container.Store(car1); </span><span style="color: #008080;">21</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> container.Commit(); </span><span style="color: #008080;">22</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"/> }</span></span><span style="color: #000000;"> </span><span style="color: #008080;">23</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"/> }</span></span><span style="color: #000000;"> </span><span style="color: #008080;">24</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">finally</span><span style="color: #000000;"> </span><span style="color: #008080;">25</span><span style="color: #000000;"><img id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_941_997_Open_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_941_997_Open_Text').style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_941_997_Closed_Image').style.display='inline'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_941_997_Closed_Text').style.display='inline';"/><img id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_941_997_Closed_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedSubBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_941_997_Closed_Text').style.display='none'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_941_997_Open_Image').style.display='inline'; document.getElementById('..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_941_997_Open_Text').style.display='inline';"/> </span><span id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_941_997_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">...</span><span id="..TPCloneCS.ZipsTPCloneExample.Cs_TPCloneExample.Cs_TestClone_1538_58_941_997_Open_Text"><span style="color: #000000;">{ </span><span style="color: #008080;">26</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> CloseDatabase(); </span><span style="color: #008080;">27</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"/> }</span></span><span style="color: #000000;"> </span><span style="color: #008080;">28</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top"/> }</span></span><span style="color: #000000;"> </span><span style="color: #008080;">29</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockEnd.gif" align="top"/> }</span></span></div></pre></div></div> </span> <span name="cs_wiki_filter" csw_filters="vb"> <div class="FormattedSourceCode"><div class="fscHeader"><span class="fscFileName">TPCloneExample.vb: <span class="fscMemberName">TestClone</span></span></div><div class="fscCode"><pre ID="..TPCloneVB.ZipsTPCloneExample.Vb_TPCloneExample.Vb_TestClone_1656_54"><div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #008080;">01</span><img id="..TPCloneVB.ZipsTPCloneExample.Vb_TPCloneExample.Vb_TestClone_1656_54_15_1034_Open_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('..TPCloneVB.ZipsTPCloneExample.Vb_TPCloneExample.Vb_TestClone_1656_54_15_1034_Open_Text').style.display='none'; document.getElementById('..TPCloneVB.ZipsTPCloneExample.Vb_TPCloneExample.Vb_TestClone_1656_54_15_1034_Closed_Image').style.display='inline'; document.getElementById('..TPCloneVB.ZipsTPCloneExample.Vb_TPCloneExample.Vb_TestClone_1656_54_15_1034_Closed_Text').style.display='inline';"/><img id="..TPCloneVB.ZipsTPCloneExample.Vb_TPCloneExample.Vb_TestClone_1656_54_15_1034_Closed_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('..TPCloneVB.ZipsTPCloneExample.Vb_TPCloneExample.Vb_TestClone_1656_54_15_1034_Closed_Text').style.display='none'; document.getElementById('..TPCloneVB.ZipsTPCloneExample.Vb_TPCloneExample.Vb_TestClone_1656_54_15_1034_Open_Image').style.display='inline'; document.getElementById('..TPCloneVB.ZipsTPCloneExample.Vb_TPCloneExample.Vb_TestClone_1656_54_15_1034_Open_Text').style.display='inline';"/><span style="color: #0000FF;">Private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Shared</span><span style="color: #000000;"> </span><span id="..TPCloneVB.ZipsTPCloneExample.Vb_TPCloneExample.Vb_TestClone_1656_54_15_1034_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">Sub TestClone()</span><span id="..TPCloneVB.ZipsTPCloneExample.Vb_TPCloneExample.Vb_TestClone_1656_54_15_1034_Open_Text"><span style="color: #0000FF;">Sub</span><span style="color: #000000;"> TestClone() </span><span style="color: #008080;">02</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> configuration </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> IConfiguration </span><span style="color: #000000;">=</span><span style="color: #000000;"> ConfigureTP() </span><span style="color: #008080;">03</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #008080;">04</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> container </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> IObjectContainer </span><span style="color: #000000;">=</span><span style="color: #000000;"> Database(configuration) </span><span style="color: #008080;">05</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">If</span><span style="color: #000000;"> container </span><span style="color: #0000FF;">IsNot</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Nothing</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Then</span><span style="color: #000000;"> </span><span style="color: #008080;">06</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Try</span><span style="color: #000000;"> </span><span style="color: #008080;">07</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> result </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> IObjectSet </span><span style="color: #000000;">=</span><span style="color: #000000;"> container.QueryByExample(</span><span style="color: #0000FF;">New</span><span style="color: #000000;"> Car(</span><span style="color: #0000FF;">Nothing</span><span style="color: #000000;">, </span><span style="color: #0000FF;">Nothing</span><span style="color: #000000;">)) </span><span style="color: #008080;">08</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> ListResult(result) </span><span style="color: #008080;">09</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> car </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> Car </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Nothing</span><span style="color: #000000;"> </span><span style="color: #008080;">10</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> car1 </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> Car </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Nothing</span><span style="color: #000000;"> </span><span style="color: #008080;">11</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">If</span><span style="color: #000000;"> result.Size() </span><span style="color: #000000;">></span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Then</span><span style="color: #000000;"> </span><span style="color: #008080;">12</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> car </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">DirectCast</span><span style="color: #000000;">(result(</span><span style="color: #800080;">0</span><span style="color: #000000;">), Car) </span><span style="color: #008080;">13</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> System.Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Retrieved car: </span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> car.ToString()) </span><span style="color: #008080;">14</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> car1 </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">DirectCast</span><span style="color: #000000;">(car.Clone(), Car) </span><span style="color: #008080;">15</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> System.Console.WriteLine(</span><span style="color: #800000;">"</span><span style="color: #800000;">Storing cloned car: </span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> car1.ToString()) </span><span style="color: #008080;">16</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> container.Store(car1) </span><span style="color: #008080;">17</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> container.Commit() </span><span style="color: #008080;">18</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">End</span><span style="color: #000000;"> </span><span style="color: #0000FF;">If</span><span style="color: #000000;"> </span><span style="color: #008080;">19</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Finally</span><span style="color: #000000;"> </span><span style="color: #008080;">20</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> CloseDatabase() </span><span style="color: #008080;">21</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">End</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Try</span><span style="color: #000000;"> </span><span style="color: #008080;">22</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">End</span><span style="color: #000000;"> </span><span style="color: #0000FF;">If</span><span style="color: #000000;"> </span><span style="color: #008080;">23</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockEnd.gif" align="top"/> </span><span style="color: #0000FF;">End Sub</span></span></div></pre></div></div> </p> </span> <p>The code above throws an exception when the cloned object is being bound to the object container. Luckily we can easily fix it by overriding #clone method and setting activator to null:<br></p> <span name="cs_wiki_filter" csw_filters="cs"> <div class="FormattedSourceCode"><div class="fscHeader"><span class="fscFileName">Car.cs: <span class="fscMemberName">Clone</span></span></div><div class="fscCode"><pre ID="..TPCloneCS.ZipsCar.Cs_Car.Cs_Clone_1986_43"><div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #008080;">1</span><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/None.gif" align="top"/><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">object</span><span style="color: #000000;"> Clone() </span><span style="color: #008080;">2</span><span style="color: #000000;"><img id="..TPCloneCS.ZipsCar.Cs_Car.Cs_Clone_1986_43_30_154_Open_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('..TPCloneCS.ZipsCar.Cs_Car.Cs_Clone_1986_43_30_154_Open_Text').style.display='none'; document.getElementById('..TPCloneCS.ZipsCar.Cs_Car.Cs_Clone_1986_43_30_154_Closed_Image').style.display='inline'; document.getElementById('..TPCloneCS.ZipsCar.Cs_Car.Cs_Clone_1986_43_30_154_Closed_Text').style.display='inline';"/><img id="..TPCloneCS.ZipsCar.Cs_Car.Cs_Clone_1986_43_30_154_Closed_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('..TPCloneCS.ZipsCar.Cs_Car.Cs_Clone_1986_43_30_154_Closed_Text').style.display='none'; document.getElementById('..TPCloneCS.ZipsCar.Cs_Car.Cs_Clone_1986_43_30_154_Open_Image').style.display='inline'; document.getElementById('..TPCloneCS.ZipsCar.Cs_Car.Cs_Clone_1986_43_30_154_Open_Text').style.display='inline';"/> </span><span id="..TPCloneCS.ZipsCar.Cs_Car.Cs_Clone_1986_43_30_154_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">...</span><span id="..TPCloneCS.ZipsCar.Cs_Car.Cs_Clone_1986_43_30_154_Open_Text"><span style="color: #000000;">{ </span><span style="color: #008080;">3</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> Car test </span><span style="color: #000000;">=</span><span style="color: #000000;"> (Car)</span><span style="color: #0000FF;">base</span><span style="color: #000000;">.MemberwiseClone(); </span><span style="color: #008080;">4</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> test._activator </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">; </span><span style="color: #008080;">5</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> test; </span><span style="color: #008080;">6</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockEnd.gif" align="top"/> }</span></span></div></pre></div></div> </span> <span name="cs_wiki_filter" csw_filters="vb"> <div class="FormattedSourceCode"><div class="fscHeader"><span class="fscFileName">Car.vb: <span class="fscMemberName">Clone</span></span></div><div class="fscCode"><pre ID="..TPCloneVB.ZipsCar.Vb_Car.Vb_Clone_2093_39"><div><!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><span style="color: #008080;">1</span><img id="..TPCloneVB.ZipsCar.Vb_Car.Vb_Clone_2093_39_7_216_Open_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockStart.gif" align="top" onClick="this.style.display='none'; document.getElementById('..TPCloneVB.ZipsCar.Vb_Car.Vb_Clone_2093_39_7_216_Open_Text').style.display='none'; document.getElementById('..TPCloneVB.ZipsCar.Vb_Car.Vb_Clone_2093_39_7_216_Closed_Image').style.display='inline'; document.getElementById('..TPCloneVB.ZipsCar.Vb_Car.Vb_Clone_2093_39_7_216_Closed_Text').style.display='inline';"/><img id="..TPCloneVB.ZipsCar.Vb_Car.Vb_Clone_2093_39_7_216_Closed_Image" src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ContractedBlock.gif" align="top" style="display: none;" onClick="this.style.display='none'; document.getElementById('..TPCloneVB.ZipsCar.Vb_Car.Vb_Clone_2093_39_7_216_Closed_Text').style.display='none'; document.getElementById('..TPCloneVB.ZipsCar.Vb_Car.Vb_Clone_2093_39_7_216_Open_Image').style.display='inline'; document.getElementById('..TPCloneVB.ZipsCar.Vb_Car.Vb_Clone_2093_39_7_216_Open_Text').style.display='inline';"/><span style="color: #0000FF;">Public</span><span style="color: #000000;"> </span><span id="..TPCloneVB.ZipsCar.Vb_Car.Vb_Clone_2093_39_7_216_Closed_Text" style="border: solid 1px #808080; background-color: #FFFFFF; display: none;">Function Clone()</span><span id="..TPCloneVB.ZipsCar.Vb_Car.Vb_Clone_2093_39_7_216_Open_Text"><span style="color: #0000FF;">Function</span><span style="color: #000000;"> Clone() </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Object</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Implements</span><span style="color: #000000;"> ICloneable.Clone </span><span style="color: #008080;">2</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Dim</span><span style="color: #000000;"> test </span><span style="color: #0000FF;">As</span><span style="color: #000000;"> Car </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">DirectCast</span><span style="color: #000000;">(</span><span style="color: #0000FF;">MyBase</span><span style="color: #000000;">.MemberwiseClone(), Car) </span><span style="color: #008080;">3</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> test._activator </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">Nothing</span><span style="color: #000000;"> </span><span style="color: #008080;">4</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/InBlock.gif" align="top"/> </span><span style="color: #0000FF;">Return</span><span style="color: #000000;"> test </span><span style="color: #008080;">5</span><span style="color: #000000;"><img src="../../../Utility/ActiPro.CodeHighligher/OutliningIndicators/ExpandedBlockEnd.gif" align="top"/> </span><span style="color: #0000FF;">End Function</span></span></div></pre></div></div> </p> </span></div> </div> <div id="footer"> This revision (2) was last Modified 2008-03-02T09:14:34 by Tetyana. </div> </body> </html>