I have a property:
public string Name {get; set;}
that receives both first name and last name as a single string. How do I separate it into different strings? Since there is no actual name yet, Name.Split(‘ ‘) does not work.
edit: if i try to create a string array such as
string[] nameSplit = Name.Split(' ');
there is a "field initializer cannot reference the non-static field, method or property Student.Name" (Student being the class that Name property belongs to).
>Solution :
You can split the Name.set value when you get it and combine the seperate values when Name.get is called. Im not checking for nulls or spaces etc here, I leave that up to you.
public string FirstName { get; set; }
public string LastName { get; set; }
public string Name
{
get
{
return $"{FirstName} {LastName}";
}
set
{
var parts = value.Split(' ');
FirstName = parts[0];
LastName = parts[1];
}
}