Thread Subject: Get method not returning anything

Subject: Get method not returning anything

From: Syed

Date: 15 Oct, 2009 21:39:05

Message: 1 of 2

Hi! here is code of my class written below. I am just trying to call method getName() to get the name but it is just displaying class name not value set by the constructor. I need hte value.
 
classdef myClass
    properties
        myName;
    end
    methods
        function this = myClass(toon)
            this.myName= toon;
        end
        
        function this = getName(this)
            this.myName;
        end
        function this=delete(this)
        end
    end
end

Subject: Get method not returning anything

From: Steven Lord

Date: 15 Oct, 2009 21:52:56

Message: 2 of 2


"Syed " <cancer216@hotmail.com> wrote in message
news:hb84po$dvv$1@fred.mathworks.com...
> Hi! here is code of my class written below. I am just trying to call
> method getName() to get the name but it is just displaying class name not
> value set by the constructor. I need hte value.
>
> classdef myClass
> properties
> myName;
> end
> methods
> function this = myClass(toon)
> this.myName= toon;
> end
>
> function this = getName(this)

As defined, this method returns the object that it received as input.
That's not what you want.

> this.myName;

The line of code above retrieves the myName property of the object you
passed in as input ... and does nothing with it. You need to assign that
property value to a value that is returned from this method in order to use
this piece of information outside the method.

Instead you might want to either just allow the user to directly access the
myName property by removing getName completely or define Name as a dependent
property and have the accessor method for that property retrieve the
contents of the myName property (which could then have its access changed to
protected or private). Instructions on how to do the latter are in the
documentation.

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

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
object oriented... Hasan Yousuf 15 Oct, 2009 17:39:23
rssFeed for this Thread

Contact us at files@mathworks.com