C# How to use a List<Type> in a service reference from a WCF

Hi Experts,

I have set an WCF, and my method asks for a IEnumerable<InvoiceDetails> collection:
Service.cs
    [ServiceContract]
    public interface IPHCWCF
    {
        [OperationContract]
        bool InvoiceDetailsStd(Header cabecalho, IEnumerable<InvoiceDetails> lstDetails); 
    }
    public class InvoiceDetails
    {
        public string Ref { get; set; }
        public string Design { get; set; }
    }

    public class Header
    {
        public string nome;
        public string morada;
    }

Open in new window

Service.svc.cs
public bool InvoiceDetailsStd(Header cab, IEnumerable<InvoiceDetails> lstDetails) {
        // code to process the data
}

Open in new window


Then i created another project to test the consume of this WCF Service as a WSDL.
I add this WCF as a service reference (http://localhost:62556/PHCWCF.svc?wsdl) and try to add the InvoiceDetails collection.
The InvoiceDetails is set as a System.Runtime.Serialization type after i add the service reference.
And I'm failing on adding items to the collection.
I'm trying this:
            phcWs.PHCWCFClient client = new phcWs.PHCWCFClient();
            bool retval;

            // Header values
            phcWs.Header cab = new phcWs.Header();
            cab.nome = "Miguel";
            cab.morada = "My address";

            phcWs.InvoiceDetails[] details;
            details[0].Ref = "ref01";
            details[0].Design = "Desc01";
            details[1].Ref = "ref02";
            details[1].Design = "Desc02";

            retval = client.InvoiceDetailsStd(cab, details);

Open in new window

It says "Use of unassigned local variable 'details'"

What am I doing wrong?
How can i send my list of items to the WCF service?

Thx in advanced,
Miguel
LVL 1
justaphaseAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
You have declared the arry but not actually created it before attempting to use it.  You need something like this:
phcWs.InvoiceDetails[] details = new InvoiceDetails[2];
Kyle AbrahamsSenior .Net DeveloperCommented:
or
List<phcWs.InvoiceDetails> Details = new List<phcWs.InvoiceDetails>();
phcWs.InvoiceDetails x = new phcWs.InvoiceDetails();
// set properties
 // x.Ref = "ref01"
 // ...
Details.add(x);
    retval = client.InvoiceDetailsStd(cab, details.ToArray());

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KinjalKumar PatelSenior Software EngineerCommented:
Collections needs to be instantiated before you can try using it. In case of Arrays we have to mention the size of it too before assigning values to it.
justaphaseAuthor Commented:
That's it!
Worked like a charm.

Thx!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.