Jump to content
  • Advertisement
Sign in to follow this  
paic

[.net] nested property and "setter"

This topic is 3296 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!