Okay, so im reading from a file and than splitting and storing the elements into different variables. enter image description here
I have a class called Person and im trying to store this info into a person object such as people.Add(new Person(firstName, lastName, address)); This works fine except for the address i cant seem to figure out. You see, i have a class called Address as well which holds street, city, state, etc. I can't seem to figure out how to store the address variables i have that are loaded with the info from the file to a new variable to add to the person object. Here is some code
PERSON CLASS
internal class Person : IComparable<Person>
{
public string FirstName { get; init; }
public string LastName { get; init; }
public Address Address { get; init; }
//constructor
public Person(string firstName, string lastName, Address address)
{
FirstName = firstName;
LastName = lastName;
Address = address;
}
}
ADDRESS CLASS
internal class Address
{
public string StreetAddress { get; init; }
public string City { get; init; }
public string State { get; init; }
public string PostalCode { get; init; }
//constructor
public Address(string streetAddress, string city, string state, string postalCode)
{
StreetAddress = streetAddress;
City = city;
State = state;
PostalCode = postalCode;
}
}
PROGRAM
string root = FileRoot.Root;
string csvFile = root + Path.DirectorySeparatorChar + "data.csv";
string psvFile = root + Path.DirectorySeparatorChar + "data.psv";
List<Person> people = new List<Person>();
using (StreamReader sr = new StreamReader(csvFile))
{
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
Console.WriteLine(line);
var split = line.Split(",");
// get and store info from file
var firstName = split[0];
var lastName = split[1];
var streetAddress = split[2];
var city = split[3];
var state = split[4];
var postalCode = split[5];
// var address = (streetAddress, city, state, postalCode);
List<Address> address = new List<Address>();
people.Add(new Person(firstName, lastName, address));
}
}