• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 535
  • Last Modified:

C# Generic List Copy

Hi,

I have a class called prices (see below). I have two generic lists of type prices, i.e. List<Prices> CurrentPrices & List<Prices> NextPrices.

My question is how best to copy one List<Prices> to the other. Basically CurrentPrices will have some price data already in it whilst calculation are being performed on this List the other List, NextPrices gets the next set of prices. So how do I copy NextPrices to CurrentPrices making sure that CurrentPrices is cleared of its previous data.

Thanks,
M

        public class Price
        {
            public DateTime dtDTime;
            public double BidOpen, BidHigh, BidLow, BidClose;
            public double AskOpen, AskHigh, AskLow, AskClose;
            public double MidOpen, MidHigh, MidLow, MidClose;
        }

Open in new window

0
mcs26
Asked:
mcs26
1 Solution
 
LIONKINGCommented:
You could implement the ICloneable interface for your class and then clone each element of the list to the new one.

Something like what's showed here: Link
0
 
käµfm³d 👽Commented:
You could do something like:

CurrentPrices = NextPrices;
NextPrices = new List<Price>(NextPrices.Count);
for (int i = 0; i < NextPrices.Count; i++) NextPrices[i] = new Price();

Open in new window

0
 
mcs26Author Commented:
Hi LionKing,

Thannks for the reply.

So do I do the following to my class

Public class Prices : ICloneable

Is there any methods I need to intialise as well?

Thanks,
M
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LIONKINGCommented:
You need to write the Clone method.

In the clone method is where you will be programming the actual "cloning" of your objects.

Here's a good place to look for this: Link
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"So how do I copy NextPrices to CurrentPrices making sure that CurrentPrices is cleared of its previous data."

Not sure what I'm missing here...sounds like you are done with the stuff in CurrentPrices and just need to move everything from NextPrices into it.  Then empty out NextPrices so it can gather the next batch?

If yes, then just do:
            CurrentPrices.Clear();
            CurrentPrices.AddRange(NextPrices.ToArray());
            NextPrices.Clear();

Open in new window

0
 
Richard LeeSoftware EnthusiastCommented:
Are you trying to achieve something like this:

while(true)
{
   // Ready to update prices
   if(!StillCalculating)
   {
      lock(NextPrices)
      {
         CurrentPrices.Clear();
         CurrentPrices.AddRange(NextPrices.ToArray());
         NextPrices.Clear();
         StillCalculating = false;
         break;
      }
   }
}

Open in new window


The above needs some work, but is this what you are after?

Avant Prime
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now