Class and Object Creation, forcing argument types
Mostra commenti meno recenti
I've made a user defined class, and I want to ensure that the arguments passed to the class constructor are of a certain variable type, and to not create an instance/object unless they are correct variable types. I have the constructor checking to see if it is a certain variable type, displaying some text saying it needs to be a certain type, and returning if it is not, but it still creates an object with empty properties. Anyone know how to prevent this? See my example below
classdef example
properties
exampleProp
end
methods
function exam = example(argument)
if ~isa(argument,'string)
disp('Argument needs to be a string')
return
end
exampleProp = argument;
end
end
My script would call
a = example(7)
Display my message and say 'Argument needs to be a string' but then i'd get an object with
a =
example
Properties:
exampleProp = []
But I don't want 'a' in this case. Any info is appreciated.
1 Commento
Mark
il 9 Ott 2014
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su String in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!