Programming |
The Stock set Method
The set
method provides a "property name" interface like the get
method. It is designed to update the number of shares, the share value, and the descriptor. The current value and the date are automatically updated.
function s = set(s,varargin) % SET Set stock properties to the specified values % and return the updated object propertyArgIn = varargin; while length(propertyArgIn) >= 2, prop = propertyArgIn{1}; val = propertyArgIn{2}; propertyArgIn = propertyArgIn(3:end); switch prop case 'NumberShares' s.numShares = val; case 'SharePrice' s.sharePrice = val; case 'Descriptor' s.asset = set(s.asset,'Descriptor',val); otherwise error('Invalid property') end end s.asset = set(s.asset,'CurrentValue',... s.numShares * s.sharePrice,'Date',date);
Note that this function creates and returns a new stock object with the new values, which you then copy over the old value. For example, given the stock object,
the following set
command updates the share price.
It is necessary to copy over the original stock object (i.e., assign the output to s
) because MATLAB does not support passing arguments by reference. Hence the set
method actually operates on a copy of the object.
The Stock get Method | The Stock subsref Method |
© 1994-2005 The MathWorks, Inc.