Class Function in array of objects works on command line but not from sequence


Redesolve

Recommended Posts

Ok, 

So here is an interesting anomaly I cant figure my way out of. Any help on this is appreciated.

I have created 2 classes Class one is subordinate to class Two

Class two has a function that when called adds a value to the array declared as a subclass (1)

**********************CODE SNIPIT CLASSDEF**************************************


define cSetAverageHistoryLength = 10

 

class typeSamplePoint
   local point = 0


   function onCreate()
      point.HistoryLength = cSetAverageHistoryLength
      
   endfunction

endclass

 


class typeLiveSensorData
   
   local Device
   
   function onCreate()
      
      private i
      
      for (i=0, i<6, i++)
         Device = new(typeSamplePoint)
      endfor
      
   endfunction
   
   function AddSample(dnum, newSample)
      
 
     Device[dnum].point[1]  =  Device[dnum].point                          //Move array down by 1
    
     Device[dnum].point[0]  =  newSample                                   //Add new sample to beginning
    
     Device[dnum].point = Device[dnum].point[0, cSetAverageHistoryLength]  //Truncate the Array to the maximun size
     

   endfunction
   
   
endclass

 

 

 

My actual function cycles through a loop to add items to the point member of each of the array

**************************************CODE of routine that adds the value**************************************

classDef()

global SampleArray
private i
private j
private numSensorType = 10
private sData


for (i=0, i<numSensorType, i ++)
   SampleArray = new(typeLiveSensorData)                    //Create array of Objects (1 for each sensor type)
   
endfor

 

 


for (i=0, i<numSensorType, i++)
         
            for (j=0, j<6, j++)                                //Loop once for every device in the sensor
            

            //Commented out for this example - use blank data
            /*   
               ptr2DataX = ReadAddrArray.PointerArray[j][0]
               ptr2DataY = ReadAddrArray.PointerArray[j][1]
               sData = SensorData[ptr2DataX][ptr2DataY]   
            */  
               
               
            sData = j  //For testing just give some sequential number
            
             ? "sData " + sData                                         
            
             ? "Sample Array :" + SampleArray.Device[j].Point
 
             //SampleArray.AddSample(j, sData)


             SampleArray[0].AddSample(0, 0)

            endfor         
      endfor

endfor 

 

Note the commented out is the actual -- I'm  just sending a value of zero to try and debug this..

 

 

Here is the problem.....

On first pass (I=0, j=0)  ->  all good .. array increase in size 

on second pass (I=0, j- 1) I get an error "Invalid object reference: Line 4....."

Here is what is strange....

when I type the my custom command ...

 

" SampleArray[0].AddSample(0, 0)"

 

...into my command / Alert window everything works fine. I can use my watch and see my arrays grow.

 

I can sub any value for I and j and value and it handles the function correctly….

 

So Why does it work in the command window but not in the sequence???

Thanks for any help or insight on this.

 

Example for Azeotech.ctl

Link to comment
Share on other sites

OK, a couple things pop out at me on a quick look:

1) when you are creating your objects, you aren't indexing:

for (i=0, i<6, i++)
      Device = new(typeSamplePoint)
endfor

The code above doesn't create an array of 6 instances of typeSamplePoint.  Instead it is creating 6 instances of typeSamplePoint and assigning it to the same variable, overwriting the previous one.  In the end you'll only end up with 1 instance.  You need to subset Device:

device = new(typeSamplePoint)

2) unfortunately you cannot combine indexing and functions with parameters in the same line, so this is invalid:

x[2].y(3)

or even:

x[2].y[4]

You can do:

x.y[4]

or even

x[4].y

Basically you can have one [] or one () in a symbol.  To get around this simply assign to a private, so x[2].y(3) becomes:

private temp = x[2]
temp.y(3)

 

Link to comment
Share on other sites

Interesting.

In fact I do index it (see the sample code I sent) but it sis not copy/paste into the caht wimdow...

so my code does read

 for (i=0, i<6, i++)
         Device = new(typeSamplePoint)
      endfor

 

for both sample point and sample array

 

Regardless the code ( as I sent in attachment) does work if I type the function in the command line.

it does not work if it is in the sequence.??

 

 

Link to comment
Share on other sites

________________________________Class DEF________________________________

define cSetAverageHistoryLength = 10



class typeSamplePoint
   local point = 0


   function onCreate()
      point.HistoryLength = cSetAverageHistoryLength
      
   endfunction
 
   
endclass




class typeLiveSensorData
   
   local Device
   
   function onCreate()
      
      private i
      
      for (i=0, i<6, i++)
         Device[i] = new(typeSamplePoint)
      endfor
      
   endfunction
   
   function AddSample(dnum, newSample)
      
     Device[dnum].point[1]  =  Device[dnum].point                          //Move array down by 1
    
     Device[dnum].point[0]  =  newSample                                   //Add new sample to beginning
    
     Device[dnum].point = Device[dnum].point[0, cSetAverageHistoryLength-1]  //Truncate the Array to the maximun size

   endfunction
   
   
endclass




_____________________________SEQ ____________________________________________


classDef()

global SampleArray
private i
private j
private numSensorType = 10
private sData


for (i=0, i<numSensorType, i ++)
   SampleArray[i] = new(typeLiveSensorData)                    //Create array of Objects (1 for each sensor type)
   
endfor






for (i=0, i<numSensorType, i++)
         
            for (j=0, j<6, j++)                                //Loop once for every device in the sensor
            
              sData = j                                        //For testing just give some sequential number
               
              SampleArray[i].AddSample(j, sData)
                
            endfor
         
      endfor

endfor 

___________________________________________________________________________________

/*
When you run thsi sequence it passes ok for the first run
subsequent run it faults.

However - if i type
"  SampleArray[0].AddSample(0, 10) "

on coammand line it happily populates my structured array
(one at a time mind you, but the function does work in that way - jsut wont work in a sequence)



 

Link to comment
Share on other sites

Much better.

But the second issue still exists:

Device[dnum].point[1]  =  Device[dnum].point 

won't work because you have two sets of [] in the same symbol.  So, for example this function:

function AddSample(dnum, newSample)
     Device[dnum].point[1]  =  Device[dnum].point                          //Move array down by 1    
     Device[dnum].point[0]  =  newSample                                   //Add new sample to beginning    
     Device[dnum].point = Device[dnum].point[0, cSetAverageHistoryLength-1]  //Truncate the Array to the maximun size
 endfunction

Should be rewritten as:

function AddSample(dnum, newSample)
     private dev = device[dnum]
     Dev.point[1]  =  Dev.point                          //Move array down by 1    
     Dev.point[0]  =  newSample                                   //Add new sample to beginning    
     Dev.point = Dev.point[0, cSetAverageHistoryLength-1]  //Truncate the Array to the maximun size
 endfunction

 

 

Link to comment
Share on other sites

________________________________Class DEF________________________________

define cSetAverageHistoryLength = 10



class typeSamplePoint
   local point = 0


   function onCreate()
      point.HistoryLength = cSetAverageHistoryLength
      
   endfunction
 
   
endclass




class typeLiveSensorData
   
   local Device
   
   function onCreate()
      
      private i
      
      for (i=0, i<6, i++)
         Device[i] = new(typeSamplePoint)
      endfor
      
   endfunction
   
   function AddSample(dnum, newSample)
      
     Device[dnum].point[1]  =  Device[dnum].point                          //Move array down by 1
    
     Device[dnum].point[0]  =  newSample                                   //Add new sample to beginning
    
     Device[dnum].point = Device[dnum].point[0, cSetAverageHistoryLength-1]  //Truncate the Array to the maximun size

   endfunction
   
   
endclass




_____________________________SEQ ____________________________________________


classDef()

global SampleArray
private i
private j
private numSensorType = 10
private sData


for (i=0, i<numSensorType, i ++)
   SampleArray[i] = new(typeLiveSensorData)                    //Create array of Objects (1 for each sensor type)
   
endfor






for (i=0, i<numSensorType, i++)
         
            for (j=0, j<6, j++)                                //Loop once for every device in the sensor
            
              sData = j                                        //For testing just give some sequential number
               
              SampleArray[i].AddSample(j, sData)
                
            endfor
         
      endfor

endfor 

___________________________________________________________________________________

/*
When you run thsi sequence it passes ok for the first run
subsequent run it faults.

However - if i type
"  SampleArray[0].AddSample(0, 10) "

on coammand line it happily populates my structured array
(one at a time mind you, but the function does work in that way - jsut wont work in a sequence)



Thanks for this, (and it does make sense),

but sadly, I still have the same issue.

 

The function works fine if I enter in the command window...

It does not work in the sequence. (Exactly as before)

?

Link to comment
Share on other sites

***Further to coments above*****
*** This does code faults *****



gloclearglobals()
classDef()

global SampleArray
private i
private j
private numSensorType = 10
private sData


for (i=0, i<numSensorType, i ++)
   SampleArray[i] = new(typeLiveSensorData)                    //Create array of Objects (1 for each sensor type)
   
endfor



for (i=0, i<numSensorType, i++)
         
         for (j=0, j<6, j++)                                  //Loop once for every device in the sensor
         
           sData = j+1                                        //For testing just give some sequential number
            
           //SampleArray[i].AddSample(j, sData)
           SampleArray[0].AddSample(0, 1)
           
             
         endfor
         
      

endfor


/*
SampleArray[0].AddSample(0, 1)
SampleArray[0].AddSample(0, 1)
SampleArray[0].AddSample(0, 1)
SampleArray[0].AddSample(0, 1)
SampleArray[0].AddSample(0, 1)
SampleArray[0].AddSample(0, 1)
*/
  
  
 ***** This code Does Not and is fine *******
  
  clearglobals()
classDef()

global SampleArray
private i
private j
private numSensorType = 10
private sData


for (i=0, i<numSensorType, i ++)
   SampleArray[i] = new(typeLiveSensorData)                    //Create array of Objects (1 for each sensor type)
   
endfor


/*
for (i=0, i<numSensorType, i++)
         
         for (j=0, j<6, j++)                                  //Loop once for every device in the sensor
         
           sData = j+1                                        //For testing just give some sequential number
            
           //SampleArray[i].AddSample(j, sData)
           SampleArray[0].AddSample(0, 1)
           
             
         endfor
         
      

endfor
*/


SampleArray[0].AddSample(0, 1)
SampleArray[0].AddSample(0, 1)
SampleArray[0].AddSample(0, 1)
SampleArray[0].AddSample(0, 1)
SampleArray[0].AddSample(0, 1)
SampleArray[0].AddSample(0, 1)
 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.