0

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.

Eric Bishard
  • 5,201
  • 7
  • 51
  • 75

2 Answers2

2

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(); }
}
Rowan Freeman
  • 15,724
  • 11
  • 69
  • 100
  • YES! As I hang my head in shame. I appreciate all of your help Rowan, this is not the first time you have helped me! I owe you a few beers! Everywhere I looked for other examples they were using the math.floor but this is way simpler. – Eric Bishard Jan 06 '14 at 05:57
0

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;
 }
Community
  • 1
  • 1
Ehsan
  • 31,833
  • 6
  • 56
  • 65
  • Can't you simplify it to `DateTime.Now.Year - StartedRealEstate.Year`? – Rowan Freeman Jan 06 '14 at 05:45
  • I could possibly get this to work, but I'm using MVC, so not using any button clicks. Sorry if my terminology is confusing, I'm new to c# – Eric Bishard Jan 06 '14 at 05:48
  • button click is just my sample code on how to use it. You can use the property directly anywhere you want. – Ehsan Jan 06 '14 at 05:50
  • Yes, that is what I'm looking for Rowan. Something simple I can use in the calculated property. But no matter how I write it I get some type of error. – Eric Bishard Jan 06 '14 at 05:51
  • @EricB why can't you simply use YearsAsAgent whereever you want – Ehsan Jan 06 '14 at 05:52
  • I see your point, so far I need this value for each agent that is part of the company. SO I'm only using this value on their profile/agent page. Does that make sense or do you have another idea in mind. YOu must know that I am a newbie. Just trying to get things done quick and easy. You mean why not use it globally right? – Eric Bishard Jan 06 '14 at 06:08
  • I simple mean that whereever, you want to get the total years of working for an agent, just call YearsAsAgent property and you will get your desired value. No need to use it globally or anything else. – Ehsan Jan 06 '14 at 07:46