1

In the following piece of code, the destructor of class TdcTestResult is called at the end of function add, and so method variable mTdcTestResults will become empty again.

How can I make the instance of TdcTestResult inside mTdcTestResults persistent?

classdef Tdc

    properties % (Access = private)
        mTestRun     = TdcTestRun;
        mTestResults = [];
    end

    methods(Access = public)

        function add(obj, componentSerialNumber, testName, testVersion, paramName, unitOfMeasureCode, paramScale, paramLimitTypeCode, paramLowerLimit, paramUpperLimit, responseValue, folderPath, isFailed, isOverridden, overriddenReason)
            if(nargin > 0)
                obj.mTestResults = [obj.mTestResults TdcTestResult];
                obj.mTestResults(end).set(componentSerialNumber, testName, testVersion, paramName, unitOfMeasureCode, paramScale, paramLimitTypeCode, paramLowerLimit, paramUpperLimit, responseValue, folderPath, isFailed, isOverridden, overriddenReason);

                obj.mTestRun.addTestResult(obj.mTestResults(end));
            end
        end
    end
end
chappjc
  • 30,359
  • 6
  • 75
  • 132
Andre K
  • 185
  • 1
  • 10

2 Answers2

0

TdcTestResult needs handle to be a superclass, otherwise your code can not work. More details in the documentation

If this does not help, please include an executable example of code.

Community
  • 1
  • 1
Daniel
  • 36,610
  • 3
  • 36
  • 69
0

I think the actual issue is that you're effectively not changing obj. If you would, the reference to the new TdcTestResult inside mTestResults should prevent the destructor from being called.

This is connected to Danial R's answer: If you don't implement Tdc as a handle subclass, your add method has to return the modified obj. Otherwise the changed you make inside add are lost.

So you'll either have to return obj and modify your calling syntax to

tdcObject = tdcObject.add(...);

or inherit from handle - which is probably the better alternative.

sebastian
  • 9,526
  • 26
  • 54