Support Board
Date/Time: Sun, 09 Feb 2025 00:52:19 +0000
Post From: Persistent Variable Scope
[2020-07-29 14:36:35] |
User462086 - Posts: 196 |
I'd like to combine the code for several custom studies into one file. It looks like functions will change the value of persistent variables, but studies will not. So, as long as I keep the persistent variable declarations inside the SCSFExport blocks (and out of the custom functions), will each variable retain its value even though the same key is being used in each SCSFExport block? See below examples. Functions will change the value of the persistent variable: #include "sierrachart.h" SCDLLName("Test_PersistentVariables_InsideFunctions") // support functions void WriteToLog( SCStudyInterfaceRef sc , SCString caption , int dataType , int intValue , float floatValue , SCDateTime dt , SCString stringValue ){ SCString DebugMessage, DateTimeString; switch(dataType){ case 0: DebugMessage.Format( "%s : %d", caption.GetChars(), intValue ); break; // integer/boolean case 1: DebugMessage.Format( "%s : %f", caption.GetChars(), floatValue ); break; // float/double case 2: DateTimeString = sc.FormatDateTime(dt); DebugMessage.Format( "%s : %s", caption.GetChars(), DateTimeString.GetChars()); break; // datetime case 3: DebugMessage.Format( "%s : %s", caption.GetChars(), stringValue.GetChars()); break; // string } sc.AddMessageToLog(DebugMessage,0); } void TestFunction(SCStudyInterfaceRef sc){ int& main = sc.GetPersistentInt(0); // using the same reference index changes the value in the main sub main = 10; } // main SCSFExport scsf_Test_PersistentVariables_InsideFunction(SCStudyInterfaceRef sc){ if (sc.SetDefaults){ sc.GraphName = "Test_PersistentVariables_InsideFunctions"; sc.AutoLoop = 0; return; } int& main = sc.GetPersistentInt(0); main = 5; TestFunction(sc); WriteToLog(sc, "main", 0, main, 0, 0, ""); } But if the persistent variable declarations are inside studies, the values don't appear to get mixed up: #include "sierrachart.h" SCDLLName("Test_PersistentVariables_SeparateStudies") // support functions void WriteToLog( SCStudyInterfaceRef sc , SCString caption , int dataType , int intValue , float floatValue , SCDateTime dt , SCString stringValue ){ SCString DebugMessage, DateTimeString; switch(dataType){ case 0: DebugMessage.Format( "%s : %d", caption.GetChars(), intValue ); break; // integer/boolean case 1: DebugMessage.Format( "%s : %f", caption.GetChars(), floatValue ); break; // float/double case 2: DateTimeString = sc.FormatDateTime(dt); DebugMessage.Format( "%s : %s", caption.GetChars(), DateTimeString.GetChars()); break; // datetime case 3: DebugMessage.Format( "%s : %s", caption.GetChars(), stringValue.GetChars()); break; // string } sc.AddMessageToLog(DebugMessage,0); } // main SCSFExport scsf_Test_PersistentVariables_SeparateStudies_1(SCStudyInterfaceRef sc){ if (sc.SetDefaults){ sc.GraphName = "Test_PersistentVariables_SeparateStudies_1"; sc.AutoLoop = 0; return; } int& main = sc.GetPersistentInt(0); int& done = sc.GetPersistentInt(1); if (done == 0){ main = 1; done = 1; } WriteToLog(sc, "main", 0, main, 0, 0, ""); } SCSFExport scsf_Test_PersistentVariables_SeparateStudies_2(SCStudyInterfaceRef sc){ if (sc.SetDefaults){ sc.GraphName = "Test_PersistentVariables_SeparateStudies_2"; sc.AutoLoop = 0; return; } int& main = sc.GetPersistentInt(0); int& done = sc.GetPersistentInt(1); if (done == 0){ main = 2; done = 1; } WriteToLog(sc, "main", 0, main, 0, 0, ""); } I'm just wondering if i can count on this behavior *always*? Many thanks!! |