Re: Overload method in a class




"Matthaeus Pilch" <Matthaeus.Pilch@xxxxxxxxxxxxxx> wrote in message
news:gab4it$f54$1@xxxxxxxxxxxxxxxxxxxxx
Hello together,

I'm programming object oriented in Matlab. I would like to overload a
method in a class:

function openFile(obj, permission)
end

function openFile(obj, filename, permission)
end

But I get the error message 'openFile appears to be incompatibly used or
redefined.' Is it possible to overlap methods in Matlab R2008a? When yes,
how?

MATLAB doesn't distinguish between methods based on the signature like this.
The error message is correct -- you're attempting to redefine openFile. If
you want to do this, you need to write one openFile method and use functions
like NARGIN and VARARGIN to determine how it was called:


classdef simpleclass73
properties
permission;
filename;
end
methods
function obj = simpleclass73(varargin)
if nargin < 1 || nargin > 2
error('simpleclass73:constructor:invalidNumberOfInputs', ...
'The simpleclass73 constructor requires 1 or 2
inputs.');
end
obj.permission = varargin{1};
if nargin > 1
obj.filename = varargin{2};
else
obj.filename = '';
end
end

function obj = openFile(obj, varargin)
switch nargin
case 2
% handle the openFile(obj, permission) case
permission = varargin{1};
filename = '';
case 3
% handle the openFile(obj, filename, permission) case
filename = varargin{1};
permission = varargin{2};
otherwise
% I don't know what you're trying to do
error('simpleclass73:openFile:unknownCase', ...
'You should call openFile with a permission or a
filename and a permission.');
end
fprintf('***** Diagnostics *****\nPermission: %s\nFilename:
%s\n', ...
permission, filename);
obj.permission = permission;
obj.filename = filename;
end
end
end


--
Steve Lord
slord@xxxxxxxxxxxxx


.



Relevant Pages

  • Re: [SLE] Nobody is home in Samba [Solved]
    ... The owner, group and world permissions are each defined by ... So chmod 0660 filename sets the file permission of filename to ... Similarly chmod 0750 filename sets the file permission of filename to ...
    (SuSE)
  • Re: [SLE] Nobody is home in Samba [Solved]
    ... > So chmod 0660 filename sets the file permission of filename to ... > Similarly chmod 0750 filename sets the file permission of filename to ...
    (SuSE)
  • Overload method in a class
    ... I would like to overload a method in a class: ... function openFile(obj, filename, permission) ... Is it possible to overlap methods in Matlab R2008a? ...
    (comp.soft-sys.matlab)
  • Overload method in a class
    ... I want to overlap a method in a class. ... function openFile(obj, filename, permission) ... Is it possible to overload methods in Matlab R2008a? ...
    (comp.soft-sys.matlab)
  • Re: File.Exists question
    ... > Thanks Nick, but it didn't seem to work. ... > FileIOPermission(FileIOPermissionAccess.AllAccess, fileName);> f.Demand; ... you aren't doing anything to> indicate>> whether or not you have permission. ... >>> using FileIOPermission to give me rights to read it, ...
    (microsoft.public.dotnet.languages.csharp)