[英]C# Array of objects
I've consumed a web service with two classes "address" and "request." 我已经使用了两个类“地址”和“请求”的Web服务。 One of the properties 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[])?
如何创建地址对象数组并将“_req.addresses”值设置为等于该对象(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:
当我尝试将城市值设置为等于字符串_q.LocationA.City时,我在第二行上得到“对象引用未设置为实例...”错误...所以这些不起作用:
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.
C#不会自动调用您的构造函数,因此这就是您在第一组代码中获得NullPointerException的原因。 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; 在第一个代码块中,您不是在数组的第一个元素中创建新的
address
对象; 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. 在第二个代码块中,您不是在
_req.addresses
成员中创建数组。 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;
使用System.Collections.Generic; using System.Linq;
使用System.Linq; using System.Text;
使用System.Text;
namespace ConsoleApplication1 { public class Address { public string Street; namespace ConsoleApplication1 {public class Address {public string Street; public string City;
公共字符串城市; public string State;
公共字符串状态; public string ZipCode;
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;
}
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.