Solved

make a copy of a DateTime variable

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

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 29

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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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:…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now