I have code that looks like this:
itemView.Question.AnswersJSON = itemView.Answer.ToJSONString();
itemView.Question.Modified = DateTime.Now;
itemView.Question.ModifiedBy = User.Identity.Name
plus many more lines where I set values for the Question class that is inside the itemView.
I think the answer is “not possible” but just putting it out as a question in case anyone knows a way.
What I would like to do is to find a way to simplify this code without repeating itemView.Question in every line.
If
Questionis aclass(not astruct), then you could assign it to a local variable, and edit that:You won’t even have to assign
qback toitemView.Question.This is because classes in C# are reference types. If you assign an instance of a reference type to a local variable, or pass it to a function, then changes to that instance will be reflected everywhere you have a reference to that same instance.
Edit
Note that the situation might be a bit murky if
Questionis a property ofitemView, rather than a field. Depending on how it is implemented, you might have to assignqback toQuestion. In such a case, this code is still much preferred to avoid calling theQuestionproperty’s getter method repeatedly.