Jump to content
  • Advertisement
Sign in to follow this  
paic

[.net] a little question about properties.

This topic is 4262 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 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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!