jrykowski

(Warning: I'm new to C# syntax! And a bit fuzzy on concepts of class, array, and object array)...

I'm trying to develop a windows client that uses a webservice. The webservices has some fairly complex data structures - including object arrays.

I've got the webservice reference in place, and it's created the class structures and everything....

Code Snippet

public partial class customerAccount {

private string acctNoField;

private location[] locationsField;

///
public string acctNo {
get {
return this.acctNoField;
}
set {
this.acctNoField = value;
}
}

///
[System.Xml.Serialization.XmlArrayItemAttribute(IsNullable=false)]
public location[] locations {
get {
return this.locationsField;
}
set {
this.locationsField = value;
}
}

:
snip
:

public partial class location {

private string descriptionField;

private tnExt primaryPhoneField;

private customerAddress[] addressesField;

private contact[] contactsField;

private order[] ordersField;

private note[] notesField;

}

But I'm having difficulty implementing the structures in code (assigning values to the array structures).

No problem with the following...

1. Create the CustomerAccount object

2. Assign acctNo value

Code Snippet

ws.wsCustomerAccountService.customerAccount CustomerAccount = new ws.wsCustomerAccountService.customerAccount();

CustomerAccount.acctNo = "123";

And no problem creating a locations object array (separate from customerAccount) and assigning values (not useful as I need to pass the entire customerAccount object containing the location objects when sending request to webservice).

The problem: Can NOT figure out how to create the locations array within the customerAccount object and assign it values...

I've tried this (declaring location array separately and then assigning to CustomerAccount):

Code Snippet

ws.wsCustomerAccountService.location[] CustomerLocation = new ws.wsCustomerAccountService.location[1];

CustomerLocation[0] = new ws.wsCustomerAccountService.location();

CustomerLocation[0].descriptionField = "blah";

CustomerAccount.locations[0] = CustomerLocation[0];

//error!

But get the NullReferenceException error: Object reference not set to an instance of an object.

Help please



Re: Visual C# Language How to use data structure containing object arrays?

decyclone

Change from :

CustomerAccount.locations[0] = CustomerLocation[0];

To :

CustomerAccount.locations[0] = CustomerLocation;


The problem here is you are not allocating memory for the array in the CustomerAccount object. So, it will be null, and that is the reason you are getting NullReferenceException.


You can also write your code as :


CustomerAccount.locations = new ws.wsCustomerAccountService.location[1];

CustomerAccount.locations = new ws.wsCustomerAccountService.location();

CustomerAccount.locations[0].descriptionField = "blah";







Re: Visual C# Language How to use data structure containing object arrays?

jrykowski

When I try that code, I get compile error on the second statement:

Error 1 Cannot implicitly convert type 'ws.wsCustomerAccountService.location' to 'ws.wsCustomerAccountService.location[]'

But just for learning purposes... Is the first statement making the declaration of the object (CustomerAccount.locations) and the second statement supposed to be allocating the memory






Re: Visual C# Language How to use data structure containing object arrays?

decyclone

Sorry,

try this :

CustomerAccount.locations = CustomerLocation;





Re: Visual C# Language How to use data structure containing object arrays?

jrykowski


Thanks for your help. I know this probably isn't pretty, but this code works!

//create the Account object
ws.wsCustomerAccountService.customerAccount CustomerAccount = new ws.wsCustomerAccountService.customerAccount();

//create the Location object arrayws.wsCustomerAccountService.location[] CustomerLocation = new ws.wsCustomerAccountService.location[1];

//allocate spaceCustomerLocation[0] = new ws.wsCustomerAccountService.location();CustomerAccount.locations = new ws.wsCustomerAccountService.location[1];

//assign the location array of Account to Location object array (above)
CustomerAccount.locations = CustomerLocation;

//set one of the values of the Account Location attributes...
CustomerAccount.locations[0].description = "blah";