# [.net] nested property and "setter"

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

## 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 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 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 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 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 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 on other sites
Oh snap. >_> I didn't know about this Immutable junk. D: I'll have to remember that. Thanks, Sneftel.

##### Share on other sites
It would probably work if you changed Vector2 to a struct, but this has other consequences too.

1. 1
2. 2
3. 3
4. 4
Rutin
16
5. 5

• 12
• 9
• 12
• 37
• 12
• ### Forum Statistics

• Total Topics
631419
• Total Posts
2999977
×