Solved

make a copy of a DateTime variable

Posted on 2011-09-28
3
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

624 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