Webservice and arrays

G

Guest

I am writing a webservice that has a method which requires an array of a custom object. I am having problems understanding why a few things are occuring

My webservice looks like this

namespace WSTes

/// <summary
/// Summary description for Service1
/// </summary
[WebService(Namespace="http://localhost")
public class Service1 : System.Web.Services.WebServic

[WebMethod
public string Test(DataField[] dfl

return dfl.Length.ToString()



public class DataFiel

public string FieldName=""
public string FieldValue=""



In my client I am accessing the method like this

localhost.Service1 proxy = new localhost.Service1();
localhost.DataField[] df = new localhost.DataField[1]
df[0] = new localhost.DataField()
df[0].FieldName="test"
df[0].FieldValue="user"
proxy.Test(df)

My question is why do I have to assign df[0] to a new instance of DataField when I declared df as a DataField array? If I remove the df[0] = new localhost.DataField(); line I get an object reference not set to an instance of an object error

Maybe I am looking at this all wrong. Is there a better way to pass an array of structured data to a webservice? I switched between using a struct and a class, but both of them act the same way (maybe struct has a bit better performance)

Any help would be greatly appreciated.
 
D

Dilip Krishnan

What you're doing by saying
localhost.DataField[] df = new localhost.DataField[1];

is that yr allocating memory for it. This is not to say you dont have to
intialize each array element. So in essence tho' you've said df[0] is
an Datafield type you havent yet initalized it.
... And there is no better way to do this :(

HTH
I am writing a webservice that has a method which requires an array of a custom object. I am having problems understanding why a few things are occuring.

My webservice looks like this:

namespace WSTest
{
/// <summary>
/// Summary description for Service1.
/// </summary>
[WebService(Namespace="http://localhost")]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string Test(DataField[] dfl)
{
return dfl.Length.ToString();
}
}

public class DataField
{
public string FieldName="";
public string FieldValue="";
}
}


In my client I am accessing the method like this:

localhost.Service1 proxy = new localhost.Service1();
localhost.DataField[] df = new localhost.DataField[1];
df[0] = new localhost.DataField();
df[0].FieldName="test";
df[0].FieldValue="user";
proxy.Test(df);


My question is why do I have to assign df[0] to a new instance of DataField when I declared df as a DataField array? If I remove the df[0] = new localhost.DataField(); line I get an object reference not set to an instance of an object error.

Maybe I am looking at this all wrong. Is there a better way to pass an array of structured data to a webservice? I switched between using a struct and a class, but both of them act the same way (maybe struct has a bit better performance).

Any help would be greatly appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top