Hi,
I have a problem regarding object oriented programming in Matlab: If I have an empty object, i.e. myObject.empty I can call methods of it and in the method manage my output for the case of an empty object, for example returning an empty array. This makes downstream code easy to handle:
obj = myObj.emptyvalues = obj.doCalculations(1,2) % Inside the doCalculation(obj, input1, input2) definition I can take care of returning an empty array in case isempty(obj)
if ~isempty(values) % proceed ...
else % handle empty cases
end
Now my problem starts with accessing properties. If I try to access the property of an empty object it just won't return anything, not an empty array, nothing the statement:
values = obj.propertyA
will just result in an error. Making my handling of downstream code more complicated. Of course I can use try and catch, but then I also have to check which error did exactly happen and so on.But what bugs me most is that in this case the getter
get.propertyA(obj)
Does not seem to be called. So there is no way to take care of this behaviour inside the class. I feel this is a strange behaviour that I can call the methods of an empty class but not its get method? Can anybody explain me why and what would be the best way to handle these cases? I know I can check first if the object is empty but since I have a lot of linked objects this is also non trivial:
values = objA.subObjB.subObjC.property1
Edit: In case it matters I'm working solemnly with handle derived classes.
Best Answer