开发者

C# Array of objects

I've consumed a web service with two classes "address" and "request." One of the properti开发者_Go百科es of the request object is an array of address objects:

request _req = new request();
_req.addresses = // expecting address[]

I know I'm doing this wrong (as I keep getting exception errors) so I'm hoping someone can help me out. How do I create an array of address objects and set the "_req.addresses" value equal to that object (address[])? I get an "object reference not set to an instance..." error on the second line, when trying to set the city value equal to the string _q.LocationA.City... so these aren't working:

    address[] _address = new address[1];
    _address[0].city = _q.LocationA.City;
    _address[0].state = _q.LocationA.State;
    _address[0].street = _q.LocationA.Address;
    _address[0].zipCode = _q.LocationA.Zip;

    request _req = new request();
    _req.addresses = _address;

And I've tried this:

    address _address = new address();
    _address.city = _q.LocationA.City;
    _address.state = _q.LocationA.State;
    _address.street = _q.LocationA.Address;
    _address.zipCode = _q.LocationA.Zip;

    request _req = new request();
    _req.addresses[0] = _address;


Your classes need to be instantiated separately from your array. C# won't call your constructor automatically, so that's why you get a NullPointerException in the first set of code. The second code fails because you're giving it a single object, instead of an array.

You essentially need to combine the two:

address[] _address = new address[1];
_address[0] = new address();
_address[0].city = _q.LocationA.City;
_address[0].state = _q.LocationA.State;
_address[0].street = _q.LocationA.Address;
_address[0].zipCode = _q.LocationA.Zip;

request _req = new request();
_req.addresses = _address;


In the first code block you are not creating a new address object in the first element of the array; thus the null reference exception when you attempt to set the city member. The fix for this is:

address[] _a = new address[1];
_a[0] = new address();
_a[0].city = ...

In the second code block you are not creating an array in the _req.addresses member. The fix for that is:

...
_req.addresses = new address[1];
_req.addresses[0] = _address;

Hope this helps!


Change:

address[] _address = new address[1];
    _address[0].city = _q.LocationA.City;
    _address[0].state = _q.LocationA.State;
    _address[0].street = _q.LocationA.Address;
    _address[0].zipCode = _q.LocationA.Zip;

To:

address[] _address = new address[1];
_address[0] = new address();
_address[0].city = _q.LocationA.City;
_address[0].state = _q.LocationA.State;
_address[0].street = _q.LocationA.Address;
_address[0].zipCode = _q.LocationA.Zip;


I would advise you to use a generic list:

List<address> _addresses = new List<address>();
_addresses.Add(_address);

request _req = new request();    
_req.addresses = _addresses;


using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace ConsoleApplication1 { public class Address { public string Street; public string City; public string State; public string ZipCode; }

public class Request
{
   public Address[] address;
}


class Program
{
    static void Main(string[] args)
    {

        Address[] address = new Address[]{ new Address {
                    Street = "140 sw 8 st",
                    City = "Miami",
                    State = "Florida",
                    ZipCode = "33122"}};

        Request req = new Request();

        req.address = address;

    }
}

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜