Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

make a copy of a DateTime variable

Posted on 2011-09-28
3
Medium Priority
?
209 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 1000 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 1000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

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:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Integration Management Part 2
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

885 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