0

I have a class called Job and that encapsulates an object of another class called SelectedItem which has a DateTime property called ItemDate;

class Job
{
    private SelectedItem m_SelectedItem = null;

    public Job() { }

    public SelectedItem Item
    {
        get { return m_SelectedItem; }
        set { m_SelectedItem = value; }
    }
}


class SelectedItem
{
    DateTime m_ItemDate = default(DateTime);

    public SelectedItem() { }

    public DateTime ItemDate
    {
        get 
        { 
            return (m_ItemDate == default(DateTime))
                ? DateTime.Now
                : m_ItemDate; 
        }
        set { m_ItemDate = value; }
    }
}

Then I have a JobManager class that has a List of Job: JobQueue;

class JobManager
{
    private ICollection<Job> m_JobQueue = null;

    public JobManager() 
    {
        m_JobQueue = new List<Job>(); 
    }

    public ReadOnlyCollection<Job> JobQueue
    {
        get { return m_JobQueue.ToList().AsReadOnly(); }
    }

    private void CreateJob(SelectedItem item)
    {
        Job newJob = new Job();
        newJob.Item = item;
        m_JobQueue.Add(newJob);
    }

    public IList<Job> GetJobsFromQueue(int quantity, ICollection<Job> ignoreList)
    {
        return (from job in JobQueue
                select job).Skip(ignoreList.Count).Take(quantity).ToList();
    }

    public void CreateJobQueue(ICollection<SelectedItem> jobData)
    {
        for (long daysCount = 100; daysCount >= 1; daysCount--)
        {
            SelectedItem item = GetRandomItem(jobData.ToList()); //This just returns a random item from the specified jobData.
            item.ItemDate = DateTime.Today.AddDays((daysCount - 1));
            CreateJob(item);
        }
    }
}

Now the issue is when I call the CreateJobQueue() from the main routine, the ItemDate is correctly inserted as in the future depending upon the daysCount value and populates the JobQueue perfectly.

But when try to retrieve a set of Jobs by calling in GetJobsFromQueue() method, the ItemDate values in the whole of JobQueue is messed up (i.e. different to what has been inserted).

Can anyone have a clue?

Omar
  • 16,329
  • 10
  • 48
  • 66
Azhar Khorasany
  • 2,712
  • 16
  • 20
  • 2
    I recommend debugging this with a unit test. – GETah May 01 '12 at 11:17
  • When you say "different to what has been inserted", can you give us an example of what was put in and what came out? – banging May 01 '12 at 11:26
  • 1
    I can't reproduce the behavior you describe. What I see is a job queue with random dates but that is not surprising given the use of `GetRandomItem`. – Martin Liversage May 01 '12 at 11:28
  • 2
    A property with lazy initialization to the current time :horror: Calling a property getter should have no observable side-effect. And as a bonus it uses local time with `DateTime` too. – CodesInChaos May 01 '12 at 11:36
  • @banging: What is put in depends upon the daysCount variable in CreateJobQueue() method. What came out is very random dates. Most of them are Current date + 5 days. Although I expect the Job Queue must have 100 items and each one has a different date as I am doing AddDays() for every item I insert in the queue. – Azhar Khorasany May 01 '12 at 12:13
  • @CodeInChaos: I know I shouldn't be changing the datetime of anything as a lazy initialization, but this is a tool written to populate test data. So it has to fake a number of dates like this. – Azhar Khorasany May 01 '12 at 12:15
  • Ok. Found the issue. It was related to me lazy initializing the ItemDate after I retrieve it from the list (jobData) in the CreateJobQueue() method. And because I am using the same jobData list again and again, the itemDate gets messed up somewhere. Thanks to @CodeInChaos for pointing out the Lazy initialization :D – Azhar Khorasany May 01 '12 at 13:05

0 Answers0