Jump to content

Question about C# properties


tec.imp
 Share

Recommended Posts

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

Link to comment
Share on other sites

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.

?? FRANCESCO CROCETTI ??

http://skaredcreations.com

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...