Ray
Ray

Reputation: 429

Objects in List<T> reference same value

I have a list of custom objects List<Slot> Each Object Slot has an array of Gene[]

Object slot

public class Slot
{
    private Gene[] _genes;
    private int _fitness;
    //...

    public Slot(int count)
    {
        _genes = InitializeArray<Gene>(count);
        Fitness = 0;
    }

    public Gene[] getChromosomes()
    {
        return Genes; //getter method
    }

    //Helper to init array
    static T[] InitializeArray<T>(int length) where T : new()
    {
        T[] array = new T[length];
        for (int i = 0; i < length; ++i)
        {
            array[i] = new T();
        }

        return array;
    }
}

Object Gene

public class Gene
{
    private DateTime _date;
    private int _tcode;
    private byte _availabe;  
    private byte _duty;  
    private int _fitness;
    //...

    public Gene()
    {
        _fitness = 0;
    }
}

Main

private List<Slot> slotsList = new List<Slot>();
//...
//...
private void init_population(int lchromeSize, int lpopulationSize)
{
    slotsList.Clear();                      
    Gene[] lstGene = InitializeArray<Gene>(lchromeSize);

    //For all slots
    for (int i = 0; i < tempInt; i++)
    {
        //for all genes

        for (int ii = 0; ii < lchromeSize; ii++)
        {
            //assign values to local variables 
            // and :
            lstGene[ii].Date = ldate;
            lstGene[ii].Tcode = lteacherCode;                    
            lstGene[ii].Availabe = lavailable;
            lstGene[ii].Duty = tempDuty;
            lstGene[ii].Fitness = 0;

        }
        //End ii For

        //Add the genes to slotsList
        Slot itemtoadd = new Slot(lchromeSize);
        itemtoadd.setChromosomes(lstGene);
        slotsList.Add(itemtoadd);
    }
}

The problem is that in every single Slot the Genes are identical and they reference the last lstGene[] that has been added to slotsList.

Where did I mess up it again ?

Upvotes: 0

Views: 94

Answers (3)

Robert J.
Robert J.

Reputation: 2701

Why don't you initialize your Gene[] lstGene inside a loop? Otherwise you are still referencing the same array over and over again if I am not mistaken

Upvotes: 1

Hans Kesting
Hans Kesting

Reputation: 39299

You need to move this line:

Gene[] lstGene = InitializeArray<Gene>(lchromeSize);

to be inside the for (int i = .. loop. You are now re-using the same array for every slot - which is what you see.

Upvotes: 2

Kirill Bestemyanov
Kirill Bestemyanov

Reputation: 11964

You should create new array for each itemtoadd.

//For all slots
for (int i = 0; i < tempInt; i++)
{
    //for all genes
    Gene[] lstGene = InitializeArray<Gene>(lchromeSize);
    for (int ii = 0; ii < lchromeSize; ii++)
    {
        //assign values to local variables 
        // and :
        lstGene[ii].Date = ldate;
        lstGene[ii].Tcode = lteacherCode;                    
        lstGene[ii].Availabe = lavailable;
        lstGene[ii].Duty = tempDuty;
        lstGene[ii].Fitness = 0;

    }
    //End ii For

    //Add the genes to slotsList
    Slot itemtoadd = new Slot(lchromeSize);
    itemtoadd.setChromosomes(lstGene);
    slotsList.Add(itemtoadd);

}

Upvotes: 3

Related Questions