0

I have this object like so [ { job: "ABC-123", task: "XYZ" }, { job: "ABC-333", task: "LAX" }];

That object is from an ajax call to here:

public List<VendorUpdateClass> updateVendorItem(List<VendorUpdateClass> edittedItems)
        {
            ConnectionClass jobs = new ConnectionClass();
            return jobs.updateVendors(edittedItems);
        }

which calls this class:

public List<VendorUpdateClass> updateVendors(List<VendorUpdateClass> items)
        {
            VendorUpdateCell = new List<VendorUpdateClass>();

            foreach (object objectItems in items)
            {
                VendorUpdateClass vendorUpdatedItem = new VendorUpdateClass();
                vendorUpdatedItem.job = objectItems.job;
                vendorUpdatedItem.task = objectItems.job;
                vendorUpdatedItem.vendor = objectItems.task;

                VendorUpdateCell.Add(vendorUpdatedItem);
            }


            return VendorUpdateCell;
        }

my issue is with my loop. I am trying to assign the job and task from the object to vendorUpdatedItem Here is the errors I am getting:

'object' does not contain a definition for 'job' and no extension method 'job' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

What am I doing wrong ?

3 Answers 3

4

On this line:

foreach (object objectItems in items) 

you are specifically treating each item in the list as an object. You know the data type so you can do it with the actual data type:

foreach (VendorUpdateClass objectItems in items)

or you could just let the compiler do it for you because the compiler knows too:

foreach (var objectItems in items)
Sign up to request clarification or add additional context in comments.

Comments

1

Change

foreach (object objectItems in items)

to

foreach (var objectItems in items)

The compiler is able to derive the correct class (VendorUpdateClass) if you use var.

Comments

1
public List<VendorUpdateClass> updateVendors(List<VendorUpdateClass> items)
    {
        VendorUpdateCell = new List<VendorUpdateClass>();

        foreach (VendorUpdateClass objectItems in items)
        {
            VendorUpdateClass vendorUpdatedItem = new VendorUpdateClass();
            vendorUpdatedItem.job = objectItems.job;
            vendorUpdatedItem.task = objectItems.job;
            vendorUpdatedItem.vendor = objectItems.task;

            VendorUpdateCell.Add(vendorUpdatedItem);
        }


        return VendorUpdateCell;
    }

Change the foreach to VendorUpdateClass instead of object since you are looping over a collection of VendorUpdateClass.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.