creating a series of objects in a loop with incrementing properties

Here's my problem.  I have an object of class V with all of the properties populated.  I need to generate a series of this V object, where certain properties (like 'date') are incremented from the last instance created.  Because I need to increment from the last one, I created preV and load it with the already incremented newV at the bottom of the loop.  Then at the top of the loop, I load the new instance of myV with preV so the date is increment from the last instance.
 
Because objects are reference types, when I increment the current instance of newV, all in the instances already in the list myVL show same the new value for the property.

BTW, the original instance is added manually before this ProcessV().  And the list is initialized previously.

       public List<V> ProcessV(Config myConfig)
        {

            var preV = myV[0];
            
            for (int i = 1; i < myConfig.Iterations; i++)
            {
                V newV = new V();
                newV = preV;
                 //  increment the date
                newV.incrementDate(myConfig.Increment);
                 //  set preV to newV so the next iteration increment last value
                preV = newV;
                myVL.Add(newV);
             }
             return myVL;
        }

Open in new window


Would changing from a List to a Dictionary with a unique key for each do what I need?
TairoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
You overwrite your new object with the old one in the loop body.

Without further information, this is pretty poor code. Using normal semantics, I would write it as:

namespace ConsoleCS
{
    using System;
    using System.Collections.Generic;

    class V
    {
        public DateTime DateTime;

        public V() { }

        public V(DateTime dateTime)
        {
            this.DateTime = dateTime;
        }

        public V(DateTime dateTime, double increment)
        {
            this.DateTime = dateTime;
            this.IncrementDateTime(increment);
        }

        public void IncrementDateTime(double increment)
        {
            this.DateTime = this.DateTime.AddDays(increment);
        }

        static public List<V> CreateObjects(Config config, V parent)
        {
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            if (parent == null)
            {
                throw new ArgumentNullException("parent");
            }

            List<V> list = new List<V>();
            for (int count = 1; count < config.Iterations; count++)
            {
                V v = new V(parent.DateTime, count * config.Increment);
                list.Add(v);
            }

            return list;
        }
    }

    class Config
    {
        public double Increment { get; set; }

        public int Iterations { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Done.");
            Console.ReadLine();
        }
    }
}

Open in new window

Fernando SotoRetiredCommented:
This line of code,  newV = preV;, overwrites the new instance with the old instance there for newV now points to the preV discarding the newV object.

If in your V class you added the following to it,
public V ShallowCopy()
{
    return (V)this.MemberwiseClone();
}

Open in new window

Then adjusted your code as follows
public List<V> ProcessV(Config myConfig)
{
    var preV = myV[0];
    
    for (int i = 1; i < myConfig.Iterations; i++)
    {
        V newV = new V();
        newV = preV.ShallowCopy();
         //  increment the date
        newV.incrementDate(myConfig.Increment);
         //  set preV to newV so the next iteration increment last value
        preV = newV;
        myVL.Add(newV);
     }
     return myVL;
}

Open in new window

It should work.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TairoAuthor Commented:
Thanks Fernando.  I wasn't familiar with the MemberwiseClone method.
Fernando SotoRetiredCommented:
Not a problem Tairo, glad I was able to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.