This is a pretty simplistic approach. Make sure you call ToString()
if the property is a string.
public string YearsAsAgent
{
get { return (DateTime.Now.Year - StartedRealEstate.Year).ToString(); }
}
문제
I'm assuming I'm pretty close. I have a value StartedAgent
that will contain a specific date entered by the user. Lets say they entered "1/1/1985" I then want to create a calculated property that I can use to display how many years since this agent first started working in Real Estate. Below is my class. I have tried to take a stab at it, but I'm coming up short. I'm using MVC 5, EF 6 & .Net 4.5 in the flavor of C#.
namespace OrlandoAppraiser.Models
{
public class Appraiser
{
public int AgentID { get; set; }
public string Name { get; set; }
public string LicenseNum { get; set; }
public DateTime StartedAgent { get; set; }
public string YearsAsAgent
{
get { return (Math.Floor((DateTime.Now - StartedRealEstate).TotalDays / 365.25D)); }
}
}
}
I have looked at some different answers, but I'm having trouble finding a way of doing this simple inside a calculated property. I know it shouldn't be that much different, but I'm getting errors with my code.
해결책
This is a pretty simplistic approach. Make sure you call ToString()
if the property is a string.
public string YearsAsAgent
{
get { return (DateTime.Now.Year - StartedRealEstate.Year).ToString(); }
}
다른 팁
This should help. Modified version of this.
DateTime _startedRealEstate = new DateTime(2012, 11, 15);
public DateTime StartedRealEstate { get { return _startedRealEstate; } set { _startedRealEstate = value; } }
public int YearsAsAgent
{
get
{
DateTime zeroTime = new DateTime(1, 1, 1);
TimeSpan span = DateTime.Now - StartedRealEstate;
int years = (zeroTime + span).Year - 1;
return years;
}
}
private void button1_Click_2(object sender, EventArgs e)
{
int totalYears = YearsAsAgent;
}