tags:

views:

95

answers:

3
    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?

+1  A: 

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.

nasufara
the pawcom to peachtree interface .dll will only return one customer at a time so I am trying to use the loop so I can pull them all out of peach tree.
Patrick
+3  A: 

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.

Matt Hamilton
Thanks Matt that looks like it will work but I was hoping there was an easier way than to transfer each property from each of the areas I'll need to work with. a way to copy the whole object vs each pice within it.
Patrick
Thanks Matt, this is working for me, may not be as fast as I was wishing for but I bet I never forget how to add items to a list after this lol.
Patrick
+1  A: 

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());
Justin Niessner
trying this I get cannot access protected member.
Patrick