473,386 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,386 developers and data experts.

Clone any object in vb 6.0

Cathode Follower
Cloning in vb6 was always a pain. It was so easy to get the code wrong, and the problems it caused were endless - normally stack overflow.
In .Net you do not have to do any cloning any more - you can simply serialise the object and then copy it into the target object. The technique is quite widely used and described - but it is interesting to use it with generics, as it means that you can use it anywhere in your application. Here is a code sample showing what to do:

Expand|Select|Wrap|Line Numbers
  1. Public Function CloneObject(Of T)(ByVal obj As T) As T
  2.         Try
  3.             Dim stream As New MemoryStream(1024)
  4.             Dim formatter As New BinaryFormatter()
  5.             formatter.Serialize(stream, obj)
  6.             stream.Seek(0, SeekOrigin.Begin) ' go back to the start
  7.             CloneObject = DirectCast(formatter.Deserialize(stream), T) 'get new object
  8.             stream.Close() ' clear down the memory
  9.         Catch excGeneric As Exception
  10.             ReportException(excGeneric)
  11.         End Try
  12.     End Function
  13.  
You have to change your class declarations to be serializable to use this code:
Expand|Select|Wrap|Line Numbers
  1. <Serializable()> Friend Class ChargeUnitType
  2.  
The call to clone any object looks similar to this:
Expand|Select|Wrap|Line Numbers
  1. Dim objClonedType As ChargeUnitType = CloneObject(ChargeUnitType) 
  2.  
Occasionally, for reasons I do not understand, the code fails on trying to deserialize, but for these cases, on the advice of Microsoft, I have included the following code in the object being serialized, and the problem has gone away:
Expand|Select|Wrap|Line Numbers
  1.  Friend Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
  2.         MyBase.New(info, context)
  3.     End Sub
  4.  
Mar 6 '08 #1
1 8590
Sorry I got the title wrong. The example and description refers to .NET ! Woops.
Mar 13 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Jason Evans | last post by:
Hi All, I am writing my own implementation of queue via a linked list, note not a LinkedList, and was running into trouble with the clone method. I was wondering if anyone could point out some...
4
by: Vincent | last post by:
Hey, I have a problem to understand the underlaying of clone method. Hope someone can help me out. Let's say, clonedObject = originalObject.clone() (syntax may be wrong, but you know what I...
6
by: Amil Hanish | last post by:
I have two classes that I have implemented ICloneable. From my top-level class, how to I clone the base class values: See "how do I clone the base class values" below. Since Clone returns an...
4
by: Steve Teeples | last post by:
I have TreeNodes in a TreeView, each contains unique data in its Tag section. I am trying to 'clone' a TreeNode and then modify the tag data of the cloned TreeNode. What I am seeing is that by...
2
by: Rob R. Ainscough | last post by:
Is there an easy way to clone an object? What I've done in the past is write my own Clone method for each class i create and then just assign property values across objects. I was hoping VS...
8
by: Noozer | last post by:
I'm looking for a way to generate a "clone" of an object. Right now I need to write a Clone function for every class that make and I'd like to have a generic routine. Instead of doing this: For...
1
by: Alex D. | last post by:
hi guys. I need to clone multiple times an object and I am succesfully cloning using the regular serialization process, using a MemoryStream. My problem is that after cloning the object more that...
16
by: Hamed | last post by:
Hello I am developing a utility to be reused in other programs. It I have an object of type Control (a TextBox, ComboBox, etc.) that other programmers use it in applications. they may set some...
14
by: Hamed | last post by:
Hello It seems that I should implement ICloneable to implement my own clone object. the critical point for me is to make a control object based on another control object that all of its event...
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.