# [.net] a little question about properties.

This topic is 4415 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 been playing with property grids for a few days now, and I still have a problem with properties. I have an object in which I declare a 2 dimension vector as a property. Then I link this object to a propertygrid. And can then browse the x and y components of my vector and modify them. But when I do that, the object is not "notified" that there was a change. So, in my case, every time the user change the x or y component, I want to send an event to another part of my program, but I don't know how to do that. I'm not sure if I'm being clear, so here is a piece of code to make my problem clearer :

// ok, this is a little 2 dimensional vector class, used for my testing
// (that's why I wrote the accessors to x and y : to be able to set breakpoints)

[TypeConverterAttribute(ExpandableObjectConverter::typeid)]
public ref struct CVector2
{
CVector2(void)
{
m_x = 0.0f;
m_y = 0.0f;
}

[NotifyParentPropertyAttribute(true)]
property float x
{
void set(float value)
{
m_x = value;
}

float get(void)
{
return(m_x);
}
}

[NotifyParentPropertyAttribute(true)]
property float y
{
void set(float value)
{
m_y = value;
}

float get(void)
{
return(m_y);
}
}

virtual String ^ ToString(void) override
{
return("" + m_x + "; " + m_y);
}

private:
float m_x;
float m_y;
};

// now, a test object. It's an instance of this object which I link to a
// property grid.

public ref class CTestObject : public System::Object
{
public:
CTestObject(void)
{
m_Vector2 = gcnew CVector2;
}

property CVector2 ^ Vector2
{
CVector2 ^ get(void)
{
return(m_Vector2);
}

void set(CVector2 ^ value)
{
// here (or elsewhere if it's not doable here) I'd like to
// sent an event to another part of the program, saying that
// this property was changed. But unfortunately, this method
// is never called when I change the x or y component of the
// vector in the propertygrid.

m_Vector2 = value;
}
}

protected:
CVector2  ^ m_Vector2;
};


I don't really know the mechanics behind the properties, so I don't know if this is even possible. Thanks for any help.

##### Share on other sites
can you just raise an event in the property setter, and have the parent subscribe to it?

I'm not familiar with the C++/CLI syntax, but in C# it's:

public class Vector2{    public float x    {        get {return _x;}        set        {            _x = value;            OnChange ();        }    }    private void OnChange ()    {        if (Changed != null)            Changed (this, EventsArgs.Empty);    }    public event EventHandler Changed;}

Then you have the parent object subscribe to the event.

I'm not sure if that helps...

##### Share on other sites
Thanks a lot for the reply ! I didn't try that, but it sure looks good ... how come I didn't think about it :/
Thx again

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632992
• Total Posts
3009755
• ### Who's Online (See full list)

There are no registered users currently online

×