All –
I am trying to dynamically (at runtime) want to create a people collection which has 3 attributes – Name, Skillset and Collection of addresses.
the problem I am facing is I cant add the address in the following line at the time of instantiate.
people.Add(new Person() { Name = "John Smith", Skillset = "Developer", _________ });
So essentially how do i combine these 3 lines into 1 so I can pass it above:
Person per = new Person();
per.Complete_Add = new List<Address>();
per.Complete_Add.Add(new Address("a", "b"));
Here is my full program:
class Program
{
static void Main(string[] args)
{
PersonViewModel personViewModel = new PersonViewModel();
}
}
public class Person
{
public string Name { get; set; }
public string Skillset { get; set; }
public List<Address> _address;
public List<Address> Complete_Add
{
get { return _address; }
set { _address = value; }
}
}
public class Address
{
public string HomeAddress { get; set; }
public string OfficeAddress { get; set; }
public Address(string _homeadd, string _officeadd)
{
HomeAddress = _homeadd;
OfficeAddress = _officeadd;
}
}
public class PersonViewModel
{
public PersonViewModel()
{
people = new List<Person>();
Person per = new Person(); \\Dont want to do this
per.Complete_Add = new List<Address>(); \\Dont want to do this
per.Complete_Add.Add(new Address("a", "b")); \\Dont want to do this
people.Add(new Person() { Name = "John Smith", Skillset = "Developer", Complete_Add = per.Complete_Add });
people.Add(new Person() { Name = "Mary Jane", Skillset = "Manager" });
people.Add(new Person() { Name = null, Skillset = null });
}
public List<Person> people
{
get;
set;
}
}
You can still do it through Property Initialisers