Silverlight Loaded Event and Object Initializers
Got into a tricky situation today.
I used the new C# Object Initializer “syntactic sugar” to initialize my object:
MyType MyObject = new MyType{MyBool=true};
At the same time I used the property as a condition in MyObject.
First I used it in the constructor, but it didn’t work since the new value of the property was given only after the constructor ran (pretty logical).
Then I used it in the Loaded Event, but the event fires way after I need the changes made by MyBool.
Three things learned:
- The Object Initializer give value to the properties after the object is instantiated.
- I thought the Loaded event fires right after the object is instantiated. It’s not.
- I’m still a Noob.
(The solution was to give parameters to my constructor and give value to the property through it. But this is not the important part.)
[…] Silverlight Loaded Event and Object Initializers […]