0

i need to return an array of a structure in a Datacontract. i cant manage to make it. i receive an error when setting the values for the array.

Here's the Datacontract declaration:

    [DataContract] 
    public class invoice_data
    {
        [DataMember]
        public Invoice_Body_Item[] invoice_body;
    }


    [StructLayout(LayoutKind.Sequential)]
    public struct Invoice_Body_Item
    {
        public string Item_Description;
        public decimal Item_Value;
    }
}

And here's the method code:

invoice_data Invoice = new invoice_data();
object tr_bl = svr.GetInvoiceData(inputparams.ck, svr.Confirm(inputparams.ck));

for (int i = ((Array)(((object[])(tr_bl))[1])).GetLowerBound(0); i <= ((Array)(((object[])(tr_bl))[1])).GetUpperBound(0); i++)
{
    Invoice.invoice_body[i].Item_Description = (string)((object[])(((object[])(((object[])(tr_bl))[1]))[i]))[0];
    Invoice.invoice_body[i].Item_Value = (decimal)((object[])(((object[])(((object[])(tr_bl))[1]))[i]))[1]; 
}
                        }

In this line i get the error "Object reference not set to an instance of an object."

Invoice.invoice_body[i].Item_Description = (string)((object[])(((object[])(((object[])(tr_bl))[1]))[i]))[0];
5
  • What does your GetInvoiceData method look like? Commented May 23, 2013 at 20:28
  • GetInvoiceData is a method from a COM Object that returns an invoice structure(tr_bl) Commented May 23, 2013 at 20:31
  • So, why not use the structure returned from the COM method? Commented May 23, 2013 at 20:41
  • tr_bl, the structure returned from the COM Object, has several "parts" like invoice_header, invoice_body, invoice_details...etc. i need to pass all this structure in a WCF Service. I am quite new to C# , is there a way to pass directly tr_bl without declaring the whole structure using [DataContract] and [Datamember]??? for example invoice_body has to items(description and value) and there is a n qty of values than can be returned for example: item A, 5.00 ; item B, 10.50; item C, 2.40 ; etc... Commented May 23, 2013 at 20:49
  • @user2386560 crazy idea...why are you trying to pass an unmanaged obj, why don't you just extract the data you need into a managed obj and pass that instead?...maybe will even shed some light into why you're getting null ref exception Commented May 24, 2013 at 4:17

1 Answer 1

1

Your struct should also be decorated with a [DataContract] + Decorate it's members with a [DataMember]:

[StructLayout(LayoutKind.Sequential)]
[DataContract]
public struct Invoice_Body_Item
{
    [DataMember]
    public string Item_Description;
    [DataMember]
    public decimal Item_Value;
}

Alternatively, you can use the [KnownTypeAttribute(typeof(Invoice_Body_Item))]

Sign up to request clarification or add additional context in comments.

10 Comments

i've tried with [DataContract] and [DataMember] ans still i get the same error on line "Invoice.invoice_body[i].Item_Description = (string)((object[])(((object[])(((object[])(tr_bl))[1]))[i]))[0];". Where shall i use the [KnownTypeAttribute(typeof(Invoice_Body_Item))]??? Thanks indeed for your help
Can you please seperate this line to as many lines as you can to see in which exact location of it it fails?
This part: (string)((object[])(((object[])(((object[])(tr_bl))[1]))[i]))[0] is working OK. It fails when trying to set the value of "Invoice.invoice_body[i].item_Description"
Are you 100% sure you send an array when you recieve the request? did you debug it? did you see the object you've returned? have you tried to look at the xml passed between the endpoints?
yes i get an array from tr_bl . just to check it i added some trace and i get to see the values. the problem is when i try to pass these values to the invoice_body datamember of the invoice_data class. for example there is another struct that i did not post thats called invoice_header that has 30 fields but its not an array, i didnt have any problem with that. i get this error with the array. maybe its something that a i'm missing to declare.
|

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.