473,813 Members | 3,166 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Binding (copy)

Hello,
I would like to know how can I copy from BindingList<obj to some other
BindingList<obj >?
I tried to pass one binding list to other via constractor but this is
copying it by reference and I want by value.
How can I do it?
Thank u very much!

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #1
4 2979
On Mon, 26 May 2008 07:36:21 -0700, csharpula csharp <cs*******@yaho o.com>
wrote:
I would like to know how can I copy from BindingList<obj to some other
BindingList<obj >?
I tried to pass one binding list to other via constractor but this is
copying it by reference and I want by value.
Passing one instance into the constructor of a new instance _is_ copying
the _BindingList_. So you don't seem to be asking the question that you
really want the answer to.

It sounds like what you really mean is that you want to clone each of the
objects in the BindingList when making the new BindingList. This may or
may not be possible, depending on the actual type of the object. If it
implements ICloneable, then you need to enumerate the original list and
call Clone() on each object to make a new instance to add to your new
BindingList.

If the type doesn't implement ICloneable, then there's not necessarily a
reliable way to do this.

You can try serializing each object to a MemoryStream, and then
deserializing it back as a new object. But whether this works for any
given type depends on whether it's serializable, and whether you really
get an exact copy of the original when it's deserialized (many types will
fail to meet one or both of those criteria).

Using reflection, you could manually do a deep copy, but this isn't a very
good general-purpose solution, as many kinds of types just aren't meant to
be duplicated. Reflection is powerful, but you run the risk of copying
something that really shouldn't have been copied if you use it.

The best solution is simply to make sure that the type of the object in
your BindingList implements ICloneable, and then clone each object
manually while creating a new BindingList.

Pete
Jun 27 '08 #2
On May 26, 7:36*pm, csharpula csharp <csharp...@yaho o.comwrote:
Hello,
I would like to know how can I copy from BindingList<obj to some other
BindingList<obj >?
I tried to pass one binding list to other via constractor but this is
copying it by reference and I want by value.
How can I do it?
Thank u very much!

*** Sent via Developersdexht tp://www.developersd ex.com***
If you are passing any list(List<T>, BindingList<Tet c) of reference
types the elements will not be created and both list elements point to
the same object in heap. Still there will be two instances of list for
List<Ti.e.
if you use List<objlist2 = new List<obj>(list1 ) and
list1.RemoveAt( 0) won't remove 0th item from list2.

But this is not the case with BindingList<T>:

BindingList<obj list1 = new BindingList<obj >;
BindingList list2 = new BindingList<obj >(list1);

now, list1.RemoveAt( 0) will remove 0th item from list1 also.

A workaround could be using an intermediate list in BindingList
constructor.

BindingList<Per sonlist2 = new BindingList<Per son>(new
List<Person>(li st));

This would enable independent operations for two instances of
BindingList. But I'd reiterate - we'll be referring to the same list
memers. Changing any item in list1 will be have effect on list2 also.
Jun 27 '08 #3
Peter Duniho wrote:
On Mon, 26 May 2008 07:36:21 -0700, csharpula csharp
<cs*******@yaho o.com wrote:
I would like to know how can I copy from BindingList<obj to some
other
BindingList<obj >?
I tried to pass one binding list to other via constractor but this
is copying it by reference and I want by value.

Passing one instance into the constructor of a new instance is
copying the BindingList. So you don't seem to be asking the
question that you really want the answer to.

It sounds like what you really mean is that you want to clone each of
the objects in the BindingList when making the new BindingList.
This may or may not be possible, depending on the actual type of the
object. If it implements ICloneable, then you need to enumerate the
original list and call Clone() on each object to make a new instance
to add to your new BindingList.

If the type doesn't implement ICloneable, then there's not
necessarily a reliable way to do this.

You can try serializing each object to a MemoryStream, and then
deserializing it back as a new object. But whether this works for
any given type depends on whether it's serializable, and whether you
really get an exact copy of the original when it's deserialized
(many types will fail to meet one or both of those criteria).

Using reflection, you could manually do a deep copy, but this isn't a
very good general-purpose solution, as many kinds of types just
aren't meant to be duplicated. Reflection is powerful, but you run
the risk of copying something that really shouldn't have been copied
if you use it.

The best solution is simply to make sure that the type of the object
in your BindingList implements ICloneable, and then clone each
object manually while creating a new BindingList.

Pete
Man, what an excellent answer.

I'd just add that there is one other option that may or may not be
suitable depending on your obj. Object provides a shallow cloning
method called MemberwiseClone (), this would be fine if you were happy
to copy value types but leave reference types pointing to the original
referenced thing. (I would however re-itterate what Peter said, if in
doubt implement ICloneable, then you are in complete control)

Cheers Tim.
--

Jun 27 '08 #4
On Mon, 26 May 2008 18:20:39 -0700, Tim Jarvis <ti*@jarvis.com .auwrote:
Man, what an excellent answer.
<blush>
I'd just add that there is one other option that may or may not be
suitable depending on your obj. Object provides a shallow cloning
method called MemberwiseClone (), this would be fine if you were happy
to copy value types but leave reference types pointing to the original
referenced thing. (I would however re-itterate what Peter said, if in
doubt implement ICloneable, then you are in complete control)
Well, MemberwiseClone () is "protected" anyway. You can't use it except
from within the class, and in that case you might as well implement
ICloneable. If the class has non-cloneable reference types, this just
pushes the problem down a layer, but who knows? Maybe that's not an issue
here (either because a shallow clone is okay, or because the class has
only cloneable members). :)

Pete
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3859
by: deko | last post by:
When using automation (and especially with early binding) I've heard it is best to use explicit references to everything. For example: Dim xlChart as Excel.Chart rather than Dim objChart as Object
5
303
by: Pete | last post by:
Hi, I want to essentially perform late binding in c#. The rough equivalent in VB (6) would be: dim x as ifcMyInterface dim SensibleClassName as string SensibleClassName = .... 'something sensible
2
1734
by: mark | last post by:
I understand that writing programs with option strict on is the best way to obtain stable applications. I have also found the applications to run much faster. Option strict on disallows late binding, so if I have an array with class level scope and I am operating on that array at procedure level I fabricate a "dummy array" and copy the class level array into it. Then after performing the task I can copy the "dummy array" back into the...
12
1558
by: Daniel Klein | last post by:
I think I've done my homework and checked around on Google Groups but this seems to be a situation not yet covered. Here is the scenario... There are two classes, Foo and Bar (actually there are more than two classes involved but two will suffice to explain the problem), and each class has a Copy() method as follows: Public Function Copy() As Object Implements ICloneable.Clone
30
2840
by: lgbjr | last post by:
hi All, I've decided to use Options Strict ON in one of my apps and now I'm trying to fix a late binding issue. I have 5 integer arrays: dim IA1(500), IA2(500), IA3(500), IA4(500), IA5(500) as integer The integers in these arrays are actually pointers to different columns of data in a text file.
2
3720
by: kogrover | last post by:
ISSUE: COM Excel Sort works with Early Binding, but not Late Binding, but py2exe only does Late Binding I have code similar to this (type from notes, so there may be a typo...) import win32com.client xl = win32com.client.Dispatch("Excel.Application") xl.Visible = False xl.ScreenUpdating = False
1
4559
by: db2group88 | last post by:
Hi, we are using db2 ESE v8.2.3 on windows, i try to use command window to do binding D:\Program Files\IBM\SQLLIB\bnd>java -cp "D:\program files\ibm\sqllib \java\db2jcc.jar" com.ibm.db2.jcc.DB2Binder -url xxx -user xxx - password xxx -s xxxx DB2Binder stopped: Failure to create connection for binding SQL code: -99999
13
1699
by: Jess | last post by:
Hello, I have some questions to do with dynamic binding. The example program is: #include<iostream> using namespace std; class A{
2
8352
by: GS | last post by:
I have installed the ms PIA for ofc XP, and followed the article http://support.microsoft.com/kb/247412/ trying to paste into a worksheet However I got late binding not allowed errors .... webOCWraooer,Copy // get the desired data into clapboard
0
9607
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10667
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10407
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10139
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9222
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6897
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5568
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3885
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.