views:

139

answers:

2

I have a variant on the by ref question. I know all about calling with the ref or our parameters and how it affects variables and their values. I had this issue with a DataTable and I want to know why a datatable is different than a simple integer variable.

I have the physical answer on how to fix the problem but I want to know why it works the way it does.

If you use simple variables it does what I expected

        int mVar1 = 1;
        int mVar2 =1;

        mVar2 = mVar1;

        mVar2 = 5;

        Console.WriteLine(mVar1.ToString());
        Console.WriteLine(mVar2.ToString());

it displays 1,5 in the console.

BUT if you do the same thing with a DataTable it makes a reference to the first datatable instead of a new value:

        DataTable mVar3 = new DataTable();
        DataTable mVar4 = new DataTable();


         // Create DataColumn objects of data types.
        DataColumn colString = new DataColumn("StringCol");
        colString.DataType = System.Type.GetType("System.String");
        mVar3.Columns.Add(colString);

        // Create DataColumn objects of data types.
        DataColumn colString2 = new DataColumn("StringCol123");
        colString2.DataType = System.Type.GetType("System.String");
        mVar4.Columns.Add(colString2);

        foreach (DataColumn tCol in mVar3.Columns)
        {
            Console.WriteLine(tCol.ColumnName);

        }
        foreach (DataColumn tCol in mVar4.Columns)
        {
            Console.WriteLine(tCol.ColumnName);

        }

                mVar4 = mVar3;

        //change mVar4 somehow and see if mVar3 changes


        foreach (DataColumn tCol in mVar4.Columns)
        {
            tCol.ColumnName = "Test";

        }

        foreach (DataColumn tCol in mVar3.Columns)
        {
            Console.WriteLine(tCol.ColumnName);

        }

        foreach (DataColumn tCol in mVar4.Columns)
        {
            Console.WriteLine(tCol.ColumnName);

        }

The console displays: StringCol StringCol123 Test Test

by saying mVar4 = mVar3 it causes mVar4 to be a reference to mVar3.

The solution to this problem is to say

DataTable mVar4 = mVar3.Copy();

So my question is: What causes a datatable to perform differently than a simple integer field. Why does it create a reference when I use mVar4 = mVar3 instead of a different copy of the DataTable?

A: 

The call mVar2 = mVar1; copies the value stored at the location of mVar1. In this scenario, that means 1 is copied in the the location mVar2. In the second situation, the value stored at mVar3 is again copied to the location of mVar4. However, in this case, because a DataTable is a reference type, the value that is copied, is the reference to the actual DataTable object.

To show this further, add the following to the end of the code you posted:

        mVar4 = new DataTable();
        // Create DataColumn objects of data types.
        DataColumn colString3 = new DataColumn("StringCol1234");
        colString2.DataType = System.Type.GetType("System.String");
        mVar4.Columns.Add(colString3);

        foreach (DataColumn tCol in mVar3.Columns)
        {
            Console.WriteLine(tCol.ColumnName); // still outputs test

        }

        foreach (DataColumn tCol in mVar4.Columns)
        {
            Console.WriteLine(tCol.ColumnName); // now outputs StringCol1234

        }

Here if you set mVar4 to a new instance of a DataTable again, then the changes made not reflected in mVar3. This is because the call mVar4 = new DataTable(); changes the reference at the location of mVar4, it does not change the object referenced by mVar4.

Timothy Carter
+2  A: 

You're running up against the difference between a reference type and a value type.

Here's an msdn article on differences.

A more descriptive answer would be that both are actually performing the same operation, the difference is that in the first example (the two integers) the assignment of mVar2 = mVar1 is assigning the value of mVar1 to mVar2, which is 1. However, in the case of the DataTable, what's actually being assigned is a memory location, not a DataTable.

Say, for instance, that the DataTable you created resides in memory location 20. That would mean that the reference mVar1 would hold a reference to that location (20). When you do the assignment mVar2 = mVar1, you're telling mVar2 to hold the same value as mVar1, so mVar2 then references to memory location 20 as well. The result being that both variables reference the same DataTable.

In order to achieve the behavior you're describing, you would indeed need to have a Copy ability, as you've stated. It would have to allocate an entirely new object, and copy the state of the previous object to the new one.

For the DataTable class you could extend it like this inside an extension method:

public static DataTable Copy(this DatTable original)
{
   var result = new DataTable();
   //assume Property1 was a property of a DataTable
   result.Property1 = original.Property1; 
   //continue copying state from original to result
   return result;
}
Joseph
I like the MSDN article as it explains what type something is. And now I see that when I hover over the DataTable datatype in visual studio it says class DataTable which according to the MSDN article is a reference type.Thank you for this info it explains everything! Please note that the datatable already has a copy method.
Jaydel Gluckie