Solved

make a copy of a DateTime variable

Posted on 2011-09-28
3
202 Views
Last Modified: 2012-05-12
Can I do this to make a copy of a DateTime variable, or does my resulting variable MyDT just point to dt which then disappears?
DateTime MyDT;

private void myfunc()
{
  DateTime dt = DateTime.Now;
  MyDT = dt;
}

Open in new window

I recall if you want to make a copy of a class instance you have to copy all the internals of the class instance, you can't just say myclasscopy = myclass.

0
Comment
Question by:deleyd
  • 2
3 Comments
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 36720772
DateTime is a structure, not an object. I.e.it is value type.
Therefore you can do that.
0
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 250 total points
ID: 36721652
you can run simple test - see the code.

Output:


MyDT: 29/09/2011 12:33:42 p.m.;
dt: 1/01/2011 12:00:00 a.m.

private void button2_Click(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            MyDT = dt;
            // change dt
            dt = new DateTime(2011, 1, 1);
            // Print both and verify dt is not pointing MyDT
            System.Diagnostics.Debug.WriteLine("MyDT: " + MyDT.ToString()+ "; "  + Environment.NewLine + "dt: " + dt.ToString());
        }

Open in new window

0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 250 total points
ID: 36722444
You are doing a copy, as you always do when you deal with a structure, the types that show as 3 little blocks glued together in the list provided by IntelliSense when you enter a type in the code window.

When you get out of the method, dt is destroyed, but you have its value in MyDT.

It is different with a class, that is represented also by 3 little blocks, but linked by small lines. In such a case, when you assign a variable to another, you copy the pointer, the address of the object in memory, and end up with 2 variables that point to the same object.

There is one exception to that rule, and it is the string, that is a class but acts as if it was a structure and always provide a copy when you assign one string to another.

You do not always have to copy all the internals to copy a class however. When class designers think that you might want to make copies, they sometimes build a method to do sot inside of the class. The DataTable class has it for instance. Thus, if you want to copy one DataTable into another, you can use the following method:
System.Data.DataTable a;
System.Data.DataTable b;
b = a.Copy ( );

Open in new window

You end up with 2 individual copies of the same structure and data.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question