Sign in to follow this  
paic

[.net] nested property and "setter"

Recommended Posts

Hi, I've got a little question about nested properties. Let's assume I have the following stuff :
[TypeConverter(typeof(ExpandableObjectConverter))]
public class Vector2
{
   [NotifyParentProperty(true)]
   public float X;

   [NotifyParentProperty(true)]
   public float Y;

   public override string ToString() { /* returns x and y in a formated string */ }
}

public class Foo
{
   private Vector2 m_Vec;
   public Vector2 Vec
   {
      set { /* a break point here */ m_Vec = value; }
      get { return m_Vec; }
   }
}


Then when I display an instance of Foo in a property grid, The Vec property shows correctly, I can expand to see both X and Y properties. When I change X or Y, the Vec property is correctly updated in the property grid, but the set method of Vec is never called. Is there a simple way to make the set method be called whenever a nested property is changed ? Or should I use delegates ? [Edited by - paic on September 9, 2009 8:45:49 AM]

Share this post


Link to post
Share on other sites
Nobody ever had this kind of case ? I can't believe it ! Or I am missing an obvious solution ? So obvious that nobody will bother telling me ?
Or maybe there simply is no easy solution to my problem ?

In any case, can you tell me ? If there's no solution except using delegate, then fine, at least I'll stop wondering if there is a simpler method :)

Thx !

Share this post


Link to post
Share on other sites
I haven't work a lot with the property grid, but I guess that if you assign X or Y the property grid will call the Foo.Vec getter and just assigns the field, so it won't assign a new value to Foo.Vec.

Share this post


Link to post
Share on other sites
Yes, exactly. And that is the cause of my problem, and I'm looking for a way to have the setter of Foo.Vec being called :)
For the moment, I'm managing a delegate called ValueChanged in my Vector2 class, and I connect it to the Foo.Vec setter. But I'm wondering if there is a better (automatic) way of doing this.

Share this post


Link to post
Share on other sites
This MSDN article, Walkthrough: Implementing a UI Type Editor, might provide a way for you. You'll want to do away with the ExpandableObjectConverter if you go this route. What I would suggest is a UI of the type UITypeEditorEditstyle.DropDown with two labels+textboxs, that would create a new Vector2 class, which would /then/ be assigned via the Vec setter.

Probably a bit more work, but I'm not seeing a cleaner solution myself at the moment. The delegate solution seems like it would be kind of...icky. :|

Share this post


Link to post
Share on other sites
One common solution is to make your Vector2 class immutable. Whenever you set Foo.Vec, you set the vector as a whole, rather than changing parts of an extant vector. In addition to solving your current problem, that avoids subtle problems that can arise from aliasing.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this