I have an object with 2 properties:
public class Request
{
public int TypeId { get; set; }
public bool isApproved { get; set; }
}
What I wish to happen is, if TypeId equals 1, I want isApproved to equal false, otherwise I want it to equal true when I create a new object. I tried the following but it was set to true for both of my objects, where I do the rule in the constructor:
public Request() {
if(this.TypeId == 1) {
this.isApproved = false;
}
this.isApproved = true;
}
var request = new Request() {
TypeId = 1
}
var request2 = new Request() {
TypeId = 2
}
I know why this occurred, its because TypeId hasn’t been set when the constructor is called, so it defaults to true. Is there anyway I can set this automatically once TypeId has been set on a newly created object?
Edit
I’d also like to have the option to change isApproved manually at a later date, so if it was set to false I can change it to true without the automatic rule I set affecting it
Try changing
to