Array in a Class


Recommended Posts


I have a class and some of the variables in there are arrays of numbers. I can't get the array to populate! I should be able to, right?

Here is my class def:


class Location
   local ID
   local string phone_number
   local string name
   local Refresh_Time
   local Active = 0
   local ADC //Array of ADC values - (0-7:Pressure sensors)
   local Relay //Array of relay values - Total of 10 Relays.(0-3:12V, 4-7:AC, 8:12V,9:AC)
   local UVT[8] = 0

global All_loc = new(Location)
All_loc.ID = 0 = "Boulder"
All_loc.phone_number = "1234556677"

private LocID = 1 //new object
All_loc[LocID] = new(Location)
All_loc[LocID].ID = LocID
All_loc[LocID].phone_number = "3034443333"
All_loc[LocID].name = "Rhungari"

1. Does "local UVT[8] = 0", initialize UVT array to 9 zeros?

2. This is how I'm trying to pupulate ADC array for instance:


global string strParse1 = Parse(strParse0,-1,",") //Creates an array 
global Parse1 = strtodouble(strParse1) //For instance: {0,4,0,0,10,1,20,2,30,3,40,4,50,5,60,6,70,7,80}
? "NumRows: " + doubletostr(NumRows(Parse1))
for (private i = 3, i < NumRows(strParse1)  , i= i +2)
					 private IdNum = Parse1[i]
					 private Measurement = Parse1[i+1]
					 ? "Sensor:" + doubletostr(IdNum) + " = " + doubletostr(Measurement) 
					 All_loc[0].ADC[IdNum] = Measurement


What I get is: All_loc[0].ADC[0] gets updated to all of those values from 10 to 80, so it's final value is 80!

(Here is my output on the command lines:

NumRows: 19

Sensor:0 = 10

Sensor:1 = 20

Sensor:2 = 30

Sensor:3 = 40

Sensor:4 = 50

Sensor:5 = 60

Sensor:6 = 70

Sensor:7 = 80)

What am I doing wrong here?

Link to comment
Share on other sites

I think that exposes a little issue with objects in DF. I'm not sure that an expression can have two subsets like that. To get around it, just do this:

private ob = all_loc[0]

ob.adc[idnum] = measurement

Remember, doing x = y when y is an object, just makes x a reference to the same object as y. That's why the above works.

Link to comment
Share on other sites

  • 3 weeks later...

Sorry. Missed it:

No, it won't. This will:

local UVT = fill(0,9)

In regular script (not a class declaration), you can also do:

private x

x[8] = 0

Since x doesn't have 9 array elements when you do x[8]=0, it will fill in the first 8 elements with 0. You just can't do this in the declaration. The declaration can only have the variable name in it.

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.