简体   繁体   中英

New to programming and having a problem with List<T>

    PAW.Btrieve oBtrieve = new PAW.Btrieve();
    PAW.CustomerClass oCustomer = new PAW.CustomerClass();
    int Status = oBtrieve.Connect("Z:\\payinc");

    if (Status == 0)
    {
        GC.Collect();
        Status = oCustomer.OpenFile();
        if (Status == 0)
        {
            Status = oCustomer.GetFirst();
            int cnt = oCustomer.RecordCount();
            List<Customer> Custlist = new List<Customer>();
            for (int i = 0; i < cnt; i++)
            {
                Custlist.Add(oCustomer);
                oCustomer.GetNext();
            }
            GridView1.DataSource = Custlist;
            GridView1.DataBind();
        }
        Status = oCustomer.CloseFile();
        GC.Collect();
    }

    oBtrieve.Disconnect();
    oBtrieve = null;

At the end of this block of code I have 28 copies of the last customer displayed in the datagrid and not the 28 diffrent customers i was wanting to see. Is there a way to just store the data from the oCustomer object and not a reffrence to the oCustomer object?

It looks like the particular API you're using reuses the same instance of CustomerClass for each customer it retrieves:

oCustomer.GetNext();

So each time you add oCustomer to your list, you're adding the same instance, and the call to "GetNext" is changing the properties of that instance.

I would suggest copying off the individual properties of oCustomer into a new instance of the class, and adding that to th list. Perhaps something like:

Custlist.Add(new CustomerClass
{
    // obviously I don't know what the properties of
    // CustomerClass are, so humour me.

    Name = oCustomer.Name,
    Address = oCustomer.Address,
    Phone = oCustomer.Phone
});

That way you're adding a different customer instance to your list each time.

You're adding oCustomer for each one. You should using your iterator, i , to access the (what I presume to be) collection in oCustomer.

I'm not sure what your class structure is, but

for (int i = 0; i < cnt; i++)
{
    Custlist.Add(oCustomer);
    oCustomer.GetNext();
}

should be:

for (int i = 0; i < cnt; i++)
{
    Custlist.Add(oCustomer[i]);
    oCustomer.GetNext();
}

Also, don't use GC.Collect() . That's just asking for trouble.

I'm guessing that this is because PAW.CustomerClass is a reference type and CustomerClass.GetNext() reads the next item into the existing object...not creating a new one.

Each time you add the object to the list, it is adding a reference to the object and NOT a copy of the object. That means when you update values on the object after adding it to the list, the object in the list will reflect those changes...and since GetNext() is making those changes to the same object each iteration, your list has 29 references to the SAME CustomerClass object.

You could try changing the following line:

Custlist.Add(oCustomer);

to

// assuming a shallow copy will work for this object
Custlist.Add(oCustomer.MemberwiseClone());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM