[RESOLVED] For Loop - Need individual Values
Hello everyone. I'm have written a class file that without this for loop will cycle through various zones (these are rooms our customers have on site) and we can assign values (such as boolean t/f, etc.).
But I need to add a feature that checks the average temp and temperature setpoints in these refrigerated rooms and then turns things on or off depending on those values (but it needs to be for each individual zone). Once I added the loop, it assigns the values (but then blankets a return over the last known value over all of the zones).
For instance in the code below DR_FanShutdown boolean will be assigned to all of the zones depending on its last known value, instead of assigning that value separately and individually for that one particular zones. Asking if anyone has any ideas that might help me in this situation.
Thanks in advance for anyone's advice.
Code:
For i As Integer = 0 To MyFacility.Count - 1
Try
If MyFacility.List(i).Name = MyEnergyEfficiency.List(0).Items(i).OwnerName Then
'If MyFacility.List(i).AvgWTemp < (CInt(MyFacility.List(i).m_SetPoint) + _TempOverride.Value) Then
'If (MyFacility.List(i).m_SetPoint.Value + _TempOverride.Value) >= MyFacility.List(i).AvgWTemp Then
If MyFacility.List(i).AvgWTemp <= (MyFacility.List(i).m_SetPoint.Value + _TempOverride.Value) Then
DR_FanShutdown = True
_ostatus.Value = "Fans Shutdown"
Fan._FanKill = True
Else
DR_FanShutdown = False
_ostatus.Value = "Temp. Override; Fans On"
Fan._FanKill = False
End If
End If
Catch ex As Exception
End Try
Next
Re: For Loop - Need individual Values
DR_FanShutdown, _ostatus.Value, and Fan._FanKill need to be global? maybe they are the cause . possible their values are being overwritten in each iteration of the loop
if they dont need to be global you could try
Code:
For i As Integer = 0 To MyFacility.Count - 1
Try
If MyFacility.List(i).Name = MyEnergyEfficiency.List(0).Items(i).OwnerName Then
Dim tempOverride As Double = _TempOverride.Value
Dim fanShutdown As Boolean
Dim oStatusValue As String
Dim fanKill As Boolean
If MyFacility.List(i).AvgWTemp <= (MyFacility.List(i).m_SetPoint.Value + tempOverride) Then
fanShutdown = True
oStatusValue = "Fans Shutdown"
fanKill = True
Else
fanShutdown = False
oStatusValue = "Temp. Override; Fans On"
fanKill = False
End If
MyFacility.List(i).UpdateFanStatus(fanShutdown, oStatusValue, fanKill)
End If
Catch ex As Exception
End Try
Next
Re: For Loop - Need individual Values
I'm unable to do it this way due to how this application was originally setup years ago.
The items aren't global, they're variables inside the same class files. DR_FanShutdown for instance is at the top of the Class as a Private Boolean. The issue is the For Loop. Once it's completed, it sets the Boolean to whatever the last zone told it to be set as, and all of the zones get changed to that value. I need a way to have them set individually within the For Loop for that particular zone at that particular time it iterated through the code and gave it a value.
Re: For Loop - Need individual Values
Post the code where you are actually defining the values in question.
Re: For Loop - Need individual Values
Quote:
Originally Posted by
jdc2000
Post the code where you are actually defining the values in question.
Here are the variables listed in the code. I reposted the code here as well.
Code:
Public Shared DR_FanShutdown As Boolean
Private _oMain As ValuePair
Private _ostatus As NameTypePair '(This variable is linked to a Class file that allows an LED light to change color depending on if it's On or Off and allows text to be displayed on the screen)
Private _TempOverride As fbInteger '(This variable is much like a normal Integer, only it has other features built into it that allow it to be displayed on the screen at the Settings Edit level & the User can change its value)
'==============Other Class File Variables=========
Public Shared _FanKill As Boolean = False 'Fans.Base.vb Class File
Public m_SetPoint As fbDouble 'Zone.Base.vb Class File
Public AvgWTemp As Double 'Zone.Base.vb Class File
'==============setup the settings=================
_TempOverride = AddInteger(2, "Temp. Override (Turn Fans Back On)", "Temp. Above Setpoint to Turn Fans Back On (Override Everything Else)", 10, 1, PropUse.Advanced, m_Node, "tempoverride", 3)
'========setup the Record/View data===========
_oMain = AddVP(VType.Boolean, True, 0)
_ostatus = _oMain.AddSub("Status:", VType.String, True, -1, "Off")
_oCntdwn = _oMain.AddSub("Remaining Time", VType.String, False, -1)
Public Overrides Sub Update()
MyBase.Update()
For i As Integer = 0 To MyFacility.Count - 1
Try
If MyFacility.List(i).Name = MyEnergyEfficiency.List(0).Items(i).OwnerName Then
'If MyFacility.List(i).AvgWTemp < (CInt(MyFacility.List(i).m_SetPoint) + _TempOverride.Value) Then
'If (MyFacility.List(i).m_SetPoint.Value + _TempOverride.Value) >= MyFacility.List(i).AvgWTemp Then
If MyFacility.List(i).AvgWTemp <= (MyFacility.List(i).m_SetPoint.Value + _TempOverride.Value) Then
DR_FanShutdown = True
_ostatus.Value = "Fans Shutdown"
Fan._FanKill = True
Else
DR_FanShutdown = False
_ostatus.Value = "Temp. Override; Fans On"
Fan._FanKill = False
End If
End If
Catch ex As Exception
End Try
Next
End Sub
Note: I feel the need to add a little more information since this seems to be a difficult task to figure out & I'm beating my head against the wall on this, so any help is so very much appreciated.
The values below end up equalling whatever the last zone in the list equals and every zone's fan either turns On or Off (incorrectly when some should be On and some shouldn't).
DR_FanShutdown's (This is a Normal Boolean that gets sent over to another class file (Public Shared, so that the main controls for this Energy Savings Feature may do other stuff.
_ostatus.Value (NameTypePair: This variable is linked to a Class file that allows an LED light to change color depending on if it's On or Off and allows text to be displayed on the screen)
Fan._FanKill (Fan = Class File Name and _FanKill = A normal boolean that has code to turn the zone's fans off if equals True. This must be this way otherwise we'll never be able to turn the fans off, but it too seems to be a blanket value that causes all zones to turn its fans On or Off depending on the last value; for the last zone in the for loop's list).
The way it normally works is that each zone runs through the class file (Named: DemandResponse; attached to the EnergyEfficiency.Base file).
I know the code for all 3 files that are linked and the other files we're sending values to work. If I don't use the For Loop and just run the code without it, its output values are correct. Basically the Class file acts as its own For Loop in a way, but once I added this new feature, the variables almost became global, even though the boolean and NameTypePair values are locally defined in the same Class I'm running this code in (I really hope this makes sense to you).
The problem is that we need to have a feature built in that if the Average Temp. in the Zone is so many degrees above its own Temperature Setpoint, the fans need to come back on (regardless if the DemandResponseControl is saying otherwise; which by the way is initiated by a press of a button & a timer starts (also settable) & once the timer ends, everything will go back to normal eventually (more controls to be added for turning them on in increments to avoid a power surge).
Re: For Loop - Need individual Values
I cant see anything obvious maybe try creating instance variables for each zone
Code:
For i As Integer = 0 To MyFacility.Count - 1
Dim currentZone = MyFacility.List(i)
Try
If currentZone.Name = MyEnergyEfficiency.List(0).Items(i).OwnerName Then
If currentZone.AvgWTemp <= (currentZone.m_SetPoint.Value + _TempOverride.Value) Then
currentZone.DR_FanShutdown = True
currentZone.Ostatus.Value = "Fans Shutdown"
currentZone.FanKill = True
Else
currentZone.DR_FanShutdown = False
currentZone.Ostatus.Value = "Temp. Override; Fans On"
currentZone.FanKill = False
End If
End If
Catch ex As Exception
End Try
Next
Not sure I can suggest anything else
Re: For Loop - Need individual Values
Is the only change that you made from the code that works the For and Next lines? If so, how were you setting the "i" cariable previously. Maybe post the code that works correctly.
You might try setting up a separate Sub that just calls the existing working Sub with whatever parameters you were using and put your For loop there.
Re: For Loop - Need individual Values
looking at this i am wondering if you created a Dict to hold the zonename and bool which you would populdate in your iteration, then after iterate the Dict and set the values accordingly. Just a thought
Re: For Loop - Need individual Values
Quote:
Originally Posted by
vbdotnut
looking at this i am wondering if you created a Dict to hold the zonename and bool which you would populdate in your iteration, then after iterate the Dict and set the values accordingly. Just a thought
I was able to figure out the solution to this problem late last night and you are correct in your assessment. I've built in a feature to get what I'm calling the 'OwnerName', which gets the name of the current zone's name that's accessing the file. But to get that name I had to be a little creative about it because the way it's built is in the Public Sub New subroutine (which comes after all the declared variables). Within this Sub I've added the OwnerName, which can only be accessed within this Sub. So I decided to create a Private String and set the Me.OwnerName to that variable.
example of code:
Code:
Public MyName As String
Public Sub New(ByVal FanNode As XmlNode, ByVal TheOwner As gItem, ByVal OwnerArray As List(Of gItem), ByVal NameAppend As String)
MyBase.New(FanNode, TheOwner, False)
Me.Name = "Fan Control" & NameAppend
Me.Settings.Name = Me.Name
Then this is the For Loop (which I've now moved into its own Friend Sub. I added a ton of comments, my apologies for not wanting to take the time to remove them, but they do explain what it's doing lol.
Code:
Friend Sub TempOverride()
'------------------------------------------------------------------------------------------------------------------------------
' NOTE:
'------------------------------------------------------------------------------------------------------------------------------
'**** Each Zone Cycles Through This File Separately. This Subroutine Checks to See if the Zone Currently Accessing
'**** This File Matches one in the Zone List. Then if it Finds a Match, it Will Set the Values to the Variables and
'**** Must Exit For Loop So That Those Values are Set to the Correct Zone Currently Accessing the File.
'------------------------------------------------------------------------------------------------------------------------------
For i As Integer = 0 To MyFacility.Count - 1
Try
'Does Zone Name Currently Accessing this File Match the One in the List?
If MyName = MyFacility.List(i).Name Then
'Does the Zone Name Match the Owner Name in Demand Response
'(Does it Have a Demand Response in Other Words, if Not Skip it)
If MyFacility.List(i).Name = MyEnergyEfficiency.List(0).Items(i).OwnerName Then
'-- Add N2 Solenoid/O2 PullDown Code HERE!
'Check if Demand Response Control Should Override the Zone Level Temp. Override (This Will Take Presidence Over the Zone's Override Settings)
If DemandResponseControl.m_EnableTmpOverride.Value = True Then
'Is the Zone's Avg. Temp. (AvgWTemp @ Zone.Base Level) Lower Than its (Temp. Setpoint + the Temp. Override Setting;
'@ the Demand Response Control (Main Control) Level of DR?
If MyFacility.List(i).AvgWTemp >= (MyFacility.List(i).m_SetPoint.Value + DemandResponseControl.mTempOverride.Value) Then
'======Fans Remain ON===============================
DR_FanShutdown = False 'Zone's Fan Shutdown Mode (Sending to DemandResponseControl in the Event We Need its Value)
_ostatus.Value = "Temp. Override; Fans On" 'Display Text on the Screen
Fan._FanKill = False 'Turn the Fans Off by Sending Boolean Value to Fan Class File
Exit For
Else
'======Fans OFF==============================
DR_FanShutdown = True 'Zone's Fan Shutdown Mode (Sending to DemandResponseControl in the Event We Need its Value)
_ostatus.Value = "Fans Shutdown" 'Display Text on the Screen
Fan._FanKill = True 'Turn the Fans Off by Sending Boolean Value to Fan Class File
Exit For
End If
Else
'Is the Zone's Avg. Temp. (AvgWTemp @ Zone.Base Level) Lower Than its (Temp. Setpoint + the Temp. Override Setting;
'(@ the Zone Level of DR)?
If MyFacility.List(i).AvgWTemp <= (MyFacility.List(i).m_SetPoint.Value + _TempOverride.Value) Then
'======Fans OFF==============================
DR_FanShutdown = True 'Zone's Fan Shutdown Mode (Sending to DemandResponseControl in the Event We Need its Value)
_ostatus.Value = "Fans Shutdown" 'Display Text on the Screen
Fan._FanKill = True 'Turn the Fans Off by Sending Boolean Value to Fan Class File
Exit For
Else
'======Fans Remain ON===============================
DR_FanShutdown = False 'Zone's Fan Shutdown Mode (Sending to DemandResponseControl in the Event We Need its Value)
_ostatus.Value = "Temp. Override; Fans On" 'Display Text on the Screen
Fan._FanKill = False 'Turn the Fans Off by Sending Boolean Value to Fan Class File
Exit For
End If
End If
End If
End If
Catch ex As Exception
End Try
Next
End Sub
Thank you all for coming out and giving your feedback! I greatly appreciate it! Once I figured out what was happening, then I knew exactly what I needed to do to fix it. I just needed to figure out how to go about doing it.
Re: For Loop - Need individual Values