Announcement

Collapse
No announcement yet.

What am I doing wrong with combining 'for each' and 'IFs' ?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    What am I doing wrong with combining 'for each' and 'IFs' ?

    Hi clever coders......

    Am getting an Error "Object variable or With block variable not set" after modifying my code below to have multiple IF/End If within a for each/next loop. I was expecting this to be possible but perhaps not, or perhaps there's something else am missing.

    Any code after the Next is ignored and the error is displayed on the console. Prior to the change I just had one If/Else/End if but couldn't work a way of doing the required check without multiple If checks, hence the change below.

    Code:
    dim array_temps() as Integer = {814,804,788,780,516,355,348,341,191,152,136,76,26,19}
    ' 22/11/18 Removed battery devices from heat demand list as they take longer to wake up and report - they are still used in the trigger though
    ' 68,160,487,95
    ' 5/1 - removed 267 was Keatons
    ' 6/1 - removed 76 was Family back
    
    dim temperature as decimal = 0
    dim setpoint as decimal = 0
    dim multilevel as decimal = 0
    dim temp_name as string = ""
    dim tempstr as string = ""
    dim devicename
    dim TRVtype
    dim TRVtypetext as string = ""
    
    
    sub Main(ByVal Parms as Object)
    
    Try
    temp_name = ""
    tempstr = ""
    
    
    
    for each devt as Integer in array_temps
               temp_name = devt
               TRVtype = hs.GetDeviceByRef(devt+2)
               TRVtypetext = TRVtype.name(hs)
    
               if TRVtypetext="Temperature"
               ' Old type TRV
               temperature = hs.DeviceValueEx(devt+2)
               setpoint = hs.DeviceValueEx(devt+5)
               multilevel = hs.DeviceValueEx(devt+3)
               devicename=hs.GetDeviceByRef(devt)
               'hs.WriteLog ("Info", TRVtypetext )
               End If
    
               if TRVtypetext="Power Management Notification"
               ' New type TRV
               temperature = hs.DeviceValueEx(devt+4)
               setpoint = hs.DeviceValueEx(devt+7)
               multilevel = hs.DeviceValueEx(devt+5)
               devicename=hs.GetDeviceByRef(devt)
               'hs.WriteLog ("Info", TRVtypetext )
               End If
    
    
            If hs.DeviceString(devt) = "Communication Failure" then
            tempstr = tempstr & temp_name & " " & " CF" & "<br>"
            End If
    
            if setpoint > temperature then
            tempstr = tempstr & devicename.name(hs) & " " & multilevel & "% " & setpoint & " SP " & temperature & " C" & "<br>"
            End If
    
    
     hs.WriteLog ("Array", tempstr)
     hs.SetDeviceString(373, tempstr,true)       
    
    next
    
       if tempstr = "" then
       tempstr = "No Demand"
       hs.WriteLog ("Array", tempstr)
       hs.SetDeviceString(373, tempstr,true)
       end if
    
    
    
    
    Catch ex As Exception
    hs.WriteLog ("Array", "Error: " & ex.Message)    
    
    
    End Try
    
    End Sub

    #2
    You are missing Then statements on your new Ifs...

    Comment


      #3
      Thanks - I've added the Then statements in (the logic actually works without it oddly) but still getting the error.......something else ???


      Code:
      dim array_temps() as Integer = {814,804,788,780,516,355,348,341,191,152,136,76,26,19}
      ' 22/11/18 Removed battery devices from heat demand list as they take longer to wake up and report - they are still used in the trigger though
      ' 68,160,487,95
      ' 5/1 - removed 267 was Keatons
      ' 6/1 - removed 76 was Family back
      
      dim temperature as decimal = 0
      dim setpoint as decimal = 0
      dim multilevel as decimal = 0
      dim temp_name as string = ""
      dim tempstr as string = ""
      dim devicename
      dim TRVtype
      dim TRVtypetext as string = ""
      
      
      sub Main(ByVal Parms as Object)
      
      Try
      temp_name = ""
      tempstr = ""
      
      
      
      for each devt as Integer in array_temps
                 temp_name = devt
                 TRVtype = hs.GetDeviceByRef(devt+2)
                 TRVtypetext = TRVtype.name(hs)
      
                 if TRVtypetext="Temperature" then
                 ' Old type TRV
                 temperature = hs.DeviceValueEx(devt+2)
                 setpoint = hs.DeviceValueEx(devt+5)
                 multilevel = hs.DeviceValueEx(devt+3)
                 devicename=hs.GetDeviceByRef(devt)
                 'hs.WriteLog ("Info", TRVtypetext )
                 End If
      
                 if TRVtypetext="Power Management Notification" then
                 ' New type TRV
                 temperature = hs.DeviceValueEx(devt+4)
                 setpoint = hs.DeviceValueEx(devt+7)
                 multilevel = hs.DeviceValueEx(devt+5)
                 devicename=hs.GetDeviceByRef(devt)
                 'hs.WriteLog ("Info", TRVtypetext )
                 End If
      
      
              If hs.DeviceString(devt) = "Communication Failure" then
              tempstr = tempstr & temp_name & " " & " CF" & "<br>"
              End If
      
              if setpoint > temperature then
              tempstr = tempstr & devicename.name(hs) & " " & multilevel & "% " & setpoint & " SP " & temperature & " C" & "<br>"
              End If
      
      
       hs.WriteLog ("Array", tempstr)
       hs.SetDeviceString(373, tempstr,true)       
      
      next
      
         if tempstr = "" then
         tempstr = "No Demand"
         hs.WriteLog ("Array", tempstr)
         hs.SetDeviceString(373, tempstr,true)
         end if
      
      
      
      
      Catch ex As Exception
      hs.WriteLog ("Array", "Error: " & ex.Message)    
      
      
      End Try
      
      End Sub

      Comment


        #4
        What do you mean the logic actually works without it? The code shouldn't have compiled before.

        Comment


          #5
          Also dim devicename should be dim devicename as string and TRVtype if you are using it...

          Comment


            #6
            Originally posted by bsobel View Post
            What do you mean the logic actually works without it? The code shouldn't have compiled before.
            Yup - compiled with no issues whatsoever ;-) also compiles okay with it in place.

            However, when I dim the devicename & TRVtype as string instead of leaving them as is I do get a compile error "devicename is not a member of string"

            Thanks for helping...........

            Comment


              #7
              Awww, sorry I assumed it was the name, its an object. You should dim at as the actual device type (Scheduler.Device I believe) since that is how you are using it.

              Comment

              Working...
              X