Thread Subject: question about value class 'setter' method

Subject: question about value class 'setter' method

From: Ian Bell

Date: 4 Nov, 2009 04:15:20

Message: 1 of 3

I am reading through the Matlab documention to understand how classes are implemented. I came across the following statement:

Value Classes. When writing methods that update an object, you must pass back the updated object and use an assignment statement. For example, a value class implementation of a set method for a property requires the object to be returned with the new value set.
A = set(A,'PropertyName',PropertyValue)

This is odd in that it appears to be saying that an instance of the class must be updated/reassigned EVERY single time a 'setter' method is called? For example, assume the class 'MyClass' has a property named 'MyStringProperty' and the method 'setMyStringProperty' which updates this property:
    mc = MyClass();
    mc = mc.setMyStringProperty( '111' );
    mc.setMyStringProperty( '222' );

I come from a C++ background and expect "MyStringProperty' to be set to '222'. The Matlab documentation implies that correct value would be '111'. Can someone clarify how I would expect to call a 'setter' method.

Thanks,

Ian

Subject: question about value class 'setter' method

From: Titus Edelhofer

Date: 4 Nov, 2009 11:15:06

Message: 2 of 3


"Ian Bell" <ibell@instepsystems.com> schrieb im Newsbeitrag
news:hcqv4o$kst$1@fred.mathworks.com...
>I am reading through the Matlab documention to understand how classes are
>implemented. I came across the following statement:
>
> Value Classes. When writing methods that update an object, you must pass
> back the updated object and use an assignment statement. For example, a
> value class implementation of a set method for a property requires the
> object to be returned with the new value set.
> A = set(A,'PropertyName',PropertyValue)
>
> This is odd in that it appears to be saying that an instance of the class
> must be updated/reassigned EVERY single time a 'setter' method is called?
> For example, assume the class 'MyClass' has a property named
> 'MyStringProperty' and the method 'setMyStringProperty' which updates this
> property:
> mc = MyClass();
> mc = mc.setMyStringProperty( '111' );
> mc.setMyStringProperty( '222' );
>
> I come from a C++ background and expect "MyStringProperty' to be set to
> '222'. The Matlab documentation implies that correct value would be
> '111'. Can someone clarify how I would expect to call a 'setter'
> method.
>
> Thanks,
>
> Ian
>

Hi Ian,

yes, that is right.
 mc = mc.setMyStringProperty( '111' ); % this really sets the property
 mc.setMyStringProperty( '222' ); % this does not
This behaviour is due to being a Value Class. If MyClass was a handle class,
the second would be sufficient.

Titus

Subject: question about value class 'setter' method

From: Steven Lord

Date: 4 Nov, 2009 14:58:45

Message: 3 of 3


"Ian Bell" <ibell@instepsystems.com> wrote in message
news:hcqv4o$kst$1@fred.mathworks.com...
>I am reading through the Matlab documention to understand how classes are
>implemented. I came across the following statement:
>
> Value Classes. When writing methods that update an object, you must pass
> back the updated object and use an assignment statement. For example, a
> value class implementation of a set method for a property requires the
> object to be returned with the new value set.
> A = set(A,'PropertyName',PropertyValue)
>
> This is odd in that it appears to be saying that an instance of the class
> must be updated/reassigned EVERY single time a 'setter' method is called?
>
> For example, assume the class 'MyClass' has a property named
> 'MyStringProperty' and the method 'setMyStringProperty' which updates this
> property:
> mc = MyClass();
> mc = mc.setMyStringProperty( '111' );
> mc.setMyStringProperty( '222' );
>
> I come from a C++ background and expect "MyStringProperty' to be set to
> '222'. The Matlab documentation implies that correct value would be
> '111'.

That depends -- is MyClass a handle class or a value class? In the former
case, it will be '222' -- in the latter case, '111'.

> Can someone clarify how I would expect to call a 'setter' method.

You might want to review the documentation that describes the differences
between MATLAB object-oriented programming and C++:

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_oop/brqzfut-1.html

as well as the section of the documentation that describes the differences
between the two types of objects, handle and value objects:

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_oop/brfylq3.html

I think the second and third sections in that first link will alleviate your
confusion.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com