Solved

make a copy of a DateTime variable

Posted on 2011-09-28
3
203 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 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