6 Replies - 560 Views - Last Post: 06 November 2012 - 06:51 AM Rate Topic: -----

#1 jase kiw  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 09-December 09

get/set property of the property?

Posted 03 November 2012 - 11:53 PM

Hello, I was wondering if there is a way I could make a get set of for a property of the property i am making.
here is an example of what i want to do. I am having trouble trying to explain what I mean.
  public Vector2 location
        {
            get
            {
                return _location;

            }
            set
            {

                _location = value;
                rectangle = new Rectangle((int)value.X, (int)value.Y, rectangle.Width, rectangle.Height);

            }
            
        }




then i want to call it like this: --note i already have the classes set up

 btnEnter.location.Y = 128;




but I get the error: Error 11 Cannot modify the return value of Button.location because it is not a variable
I understand why i am getting this error but I don't know how to set the property to accept sub properties.. can anyone help me with this? I need several commands to happen when these things happen, that is why I need a get/set. I was thinking about making a total separate get/set for just Y and X instead of location.X, and location.Y but i decided that would be less organized. Thank you for any help!

Is This A Good Question/Topic? 0
  • +

Replies To: get/set property of the property?

#2 Tinix  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 55
  • Joined: 07-May 11

Re: get/set property of the property?

Posted 04 November 2012 - 12:58 AM

Quoting MSDN on this error:

Quote

This error occurs because value types are copied on assignment. When you retrieve a value type from a property or indexer, you are getting a copy of the object, not a reference to the object itself. The copy that is returned is not stored by the property or indexer because they are actually methods, not storage locations (variables).

It basically means that the value that is retruned by get is read-only as it has no varaible assigned to it (it is only on stack). This does not apply to classes as C# passes those through reference not by value.

This post has been edited by Tinix: 04 November 2012 - 01:06 AM

Was This Post Helpful? 1
  • +
  • -

#3 jase kiw  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 09-December 09

Re: get/set property of the property?

Posted 04 November 2012 - 09:47 AM

So how would I set it up to have certain commands happen when the value of location or a subvalue of location is changed?
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: get/set property of the property?

Posted 04 November 2012 - 10:33 AM

Do what the framework generally does and raise an event, or implement INotifyProperyChanged interface.
Was This Post Helpful? 0
  • +
  • -

#5 jase kiw  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 09-December 09

Re: get/set property of the property?

Posted 04 November 2012 - 10:47 AM

How may I raise an event or use this INotifyPropertyChanged?
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: get/set property of the property?

Posted 04 November 2012 - 10:57 AM

Read the documentation in the provided link.
Was This Post Helpful? 2
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: get/set property of the property?

Posted 06 November 2012 - 06:51 AM

If you want code to run when the button moves, why not just subscribe to the Move event for the control? That would fire when the button is moved, which is effectively when the location changes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1