how can I change the "get" and "set" on a Model Property?

egucciar picture egucciar · Mar 26, 2012 · Viewed 8.5k times · Source

Currently I have a DateTime property for a model. I am using the telerik MVC framwork but I am using a DateTime property and the editor column for this property is auto-generated so there is no code which controls it in my View or Controller. On Telerik's website there are instructions on how to set the default date time for a date time picker but this picker isn't initiated anywhere because it is in a column. The problem is I want to set the DateTime to be the current date and time if it isn't already specified. Currently, the code for the model looks like this:

 public DateTime CreatedDate { get; set;}

I want it to do something more like this:

public DateTime CreatedDate
        {
            get
            {
                if (QuestionID != 0)
                {
                    return this.CreatedDate;
                }
                else
                {
                    return DateTime.Now;
                }

            }
            set { CreatedDate = value; }
        }

this way it will return the DateTime that is stored for this Question if the ID exists. If you are creating a new Question, it gets the current DateTime.

The problem is here with the Set. When I try to load up a screen, set get's a Stack Overflow. I'm really unfamilular with this kind of code, so I have no idea how to work with it.

Before we were not working with the model, and instead using JQuery to get the CreatedDate's data and set it to the current date time. The issue with that is when you go to the "picker" part of the date time, it goes to the default date time rather than the current one. this is why I want to set it through the Model, View, or Controller, and not with Jquery.

Let me know if you can help me understand Gets and Sets in the model!

Answer

Justin Pihony picture Justin Pihony · Mar 26, 2012

You need to have a private property you are using behind the scenes.

    private DateTime _createdDate;
    public DateTime CreatedDate
    {
        get
        {
            if (QuestionID != 0)
            {
                return _createdDate;
            }
            else
            {
                return DateTime.Now;
            }

        }
        set { _createdDate = value; }
    }

You are getting an overflow because you are doing something like this currently:

CreatedDate = 1/1/2012;
...which then calls
CreatedDate = 1/1/2012;
...which then calls
CreatedDate = 1/1/2012
..You get the point (it is continuously setting itself until the stack overflows)

Auto implemented properties ({get;set;}) actually use a private variable behind the scenes. If you were to look at the IL, then you would see that it actually breaks that simple {get;set;} into a getter/setter based on a generated private variable. They are just a type of "compiler magic" to cut down on boilerplate code of having to create a private variable when there is no real logic in the getter/setter. If you have logic, then you need to implement this private variable yourself.