I am trying to add the object to the list of objects.
The below code I had written is :
Address adr=new Address();
List<Address> addr = new List<Address>();
foreach(var item in TransportAddress)
{
adr.Address1=item.Address1;
adr.Address2=item.Address2;
adr.PhoneNumber=item.PhoneNumber;
addr.Add(adr);
}
I the above code I had initialized the object 'adr' only once, so that it is overwritting the object data.
The code should be as below .
List<Address> addr = new List<Address>();
foreach(var item in TransportAddress)
{
Address adr=new Address();
adr.Address1=item.Address1;
adr.Address2=item.Address2;
adr.PhoneNumber=item.PhoneNumber;
addr.Add(adr);
}
The below code I had written is :
Address adr=new Address();
List<Address> addr = new List<Address>();
foreach(var item in TransportAddress)
{
adr.Address1=item.Address1;
adr.Address2=item.Address2;
adr.PhoneNumber=item.PhoneNumber;
addr.Add(adr);
}
I the above code I had initialized the object 'adr' only once, so that it is overwritting the object data.
The code should be as below .
List<Address> addr = new List<Address>();
foreach(var item in TransportAddress)
{
Address adr=new Address();
adr.Address1=item.Address1;
adr.Address2=item.Address2;
adr.PhoneNumber=item.PhoneNumber;
addr.Add(adr);
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.