473,395 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

2-dimensional table copy

Hello everyone! I'm new to C# and i have a question:

If there is a two-dimensional table of objects A , can i copy it to another table B by simply assigning A to B?

e.g
public Object[,] A;
A= new Object[ 10, 10];

public Object[,] B ;
B= A;

Does this work?

Thanks!
Nov 16 '06 #1
2 1202
Hi there,

The example that you gave would result in Array variables A and B pointing to the same array (and therefore the same objects). The code below proves this:

Expand|Select|Wrap|Line Numbers
  1.  
  2.     // Declare and initialise array A
  3.     Object[,] A;
  4.     A = new Object[10, 10];
  5.  
  6.     // Set a value
  7.     A[2, 4] = "Hi there";
  8.  
  9.     // Declare another array, B
  10.     Object[,] B;
  11.  
  12.     // Set B's reference to array A (shallow copy - same as Clone())
  13.     B = A;
  14.  
  15.     // Array variables A and B now point to the same array, 
  16.     // and therefore, the same objects.
  17.  
  18.     Console.WriteLine("A[2,4]={0}\nB[2,4]={1}",
  19.         A[2, 4], B[2, 4]);
  20.  
  21.     // Change a value in A
  22.     A[2, 4] = "A new value";
  23.  
  24.     // See if the value in A has affected
  25.     // the value in B.
  26.     Console.WriteLine("A[2,4]={0}\nB[2,4]={1}",
  27.                     A[2, 4], B[2, 4]);
  28.  
  29.     // Do the references of A and B match?
  30.     Console.WriteLine("Does A[2,4] reference "
  31.             + "the same object as B[2,4]? Answer={0}",
  32.             (Object.ReferenceEquals(A[2, 4], B[2, 4])
  33.             == true) ? "Yes" : "No");
  34.  
  35.  
Arrays are reference types (not Value types). Reference types simply refer to objects, they do not hold the value of an object. Thus in the example, assigning B with the value of A simply means that B is assigned a copy of A's reference to an array. They both then refer to the same array.

It is important to understand that variable B is not provided with a fresh stand alone copy of the array that variable A refers to. Both variables now refer to the same array and thus the same objects in the array.

The only way to get a completely stand alone copy of a two-dimensional array and its elements is to manually copy every element in the array to a new array.

Be careful when hand copying array elements. If the array holds value types (such as int, long, char etc) then simply assigning them to a location in the new array will work fine. However, if (like in the example above) the array contains reference types (Objects) then obtaining a shallow copy of each element raises the same problem as the array copy issue - i.e. you'll be copying the references to objects and not copying the objects themselves.

Each object in the array will have to be DEEP copied using ICloneable interface.

I refer you to:

http://www.thescripts.com/forum/thread233829.html

Hope this helps a bit.
Nov 16 '06 #2
Thanks a lot, this helps!
Nov 18 '06 #3

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

Similar topics

2
by: Dianna K. | last post by:
I have a dataview which I am assigning to a Datatable (I am doing this because currently Crystal Reports will not accept a dataview as a Reportsource -- The recommended work-around is to assign...
2
by: Johnny Ruin | last post by:
Hi, I simply want to create a duplicate of an existing table with a different name. I've tried the "select * into newtable from orgtable", which works great, except that it doesn't mark the...
0
by: Alex | last post by:
Hi, I have two db servers primary/backup for which I would like to synchronize a few tables. Basically I need to check every 30 - 60 minutes if on the primary table an update has beend one and...
2
by: news.hp.com | last post by:
I have situation where I need to copy multiple records (only certain fields) from a Rules table to an Events table based on a selection identified in a combo box. When the selection is made in a...
2
by: HB2 | last post by:
I use to be able to highlight several records in a table, copy them and paste new records. Now when I right click the records the copy and paste functions are disabled. How do I enable them? ...
4
by: Wayne Wengert | last post by:
When a specific event occurs I want to make backup copies of 2 tables using names like "BkupTbla081204.xml" and "BkupTblb081204.xml". What is the best way to do this? I suspect there is some simple...
4
by: GTi | last post by:
I have two identical tables with one IDENTITY column and several other columns. I have tested the COPY * INTO table2 FROM table1 WHERE xx but it requers that table2 does not exist. TABLE2 is a...
1
by: tdv | last post by:
Please help me with a script to copy a table in MS SQL and overwrite the same table in another db
8
by: pechar | last post by:
Hi all, I created an application to copy certain tables from a database on MSSQL server to another database on MYSQL server which is on another server (very very very far away). The process works...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...

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.