tec.imp Posted April 22, 2010 Share Posted April 22, 2010 Hey there, i bumped the other day into a little problem regarding C#'s properties. Let's say i do have this setup: public class Point { public float X; public float Y; } public class Control { protected Point m_Position = new Point(); public Point Position { get { return m_Position; } set { m_Position = value; } // reorganize internal structure.. reorganize(); } protected reorganize() { // do some stuff } } This is all fine, but when it comes to usage, i could write something like: Control myControl = new Control(); myControl.Position.X = 1.0f; The thing is, my Control class wont recognize that the Position has been changed because set hasn't been called. So i guess my question is, is there a way to make Control aware of any Position changes? Thanks in advance! Mfg Imp Quote Link to comment Share on other sites More sharing options...
ZioRed Posted April 22, 2010 Share Posted April 22, 2010 You forgot to name of the property: public Position { ... } should be: public Point Position { ... } .. or is it a typo? (however I don't see the closure bracket of Position property after the "set") Quote ?? FRANCESCO CROCETTI ?? http://skaredcreations.com Link to comment Share on other sites More sharing options...
tec.imp Posted April 22, 2010 Author Share Posted April 22, 2010 Thanks for pointing this out. And yes it was a typo Quote Link to comment Share on other sites More sharing options...
ZioRed Posted April 22, 2010 Share Posted April 22, 2010 Oh ok perhaps I understand, the "set" you intercept is for "Position" changes, not the Position's member changes. For that you may want to define a delegate in the Point class for event handling and attach an event handler in the Position set. Take a look at here if you like to implement an INotifyPropertyChanged interface for your control. Quote ?? FRANCESCO CROCETTI ?? http://skaredcreations.com Link to comment Share on other sites More sharing options...
L B Posted April 22, 2010 Share Posted April 22, 2010 EDIT: public class Point { private float x; public float X { get { return x; } set { x = value; reorganize(); } private float y; public float Y { get { return y; } set { y = value; reorganize(); } } Quote Link to comment Share on other sites More sharing options...
tec.imp Posted April 22, 2010 Author Share Posted April 22, 2010 Thanks ZioRed, thing is delegates produce some overhead structural and memory wise and for such things like Point(x,y), Size(Width, Height), etc. I think it's too much. Well one solution I found was immutable objects which is what I implemented to get rid of the overwriting and not get notified (setter not called) ****. I really wonder why C# does not implement such a behavior in the first place... Mfg Imp Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.