Silverlight Loaded Event and Object Initializers

code, silverlight — Szili @ 12:38 pm

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.)

1 Comment »

  1. […] Silverlight Loaded Event and Object Initializers […]

    Pingback by Silverlight Emotional Handbook < Szili — September 21, 2007 @ 11:47 am

RSS feed for comments on this post. TrackBack URI

Leave a comment

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2010 Szili | powered by WordPress with Barecity