<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel>
        <title>Using STATIC variables within a procedure</title>
        <description> There is one big feature i am missing in WinDev:
the use of STATIC variables in a procedure to preserve specific values from one call to another.

Actualy there is no other alternative than using GLOBALs, however this is considered as a bad programming pratice in most languages i know, and it doesn&amp;#039;t help to create modular procedures that can be reused from one project to another.

To work around this problem, i have written a couple of functions (see the code below), but if you have a better solution i would be glad to know about it.

Thank you!


_________________________________________________________________

SetStaticValue
PROCEDURE SetStaticValue(LOCAL sUseString is string, LOCAL nValue is int)
sUseString += &amp;quot;|&amp;quot;
nhMain is int = Handle(MyWindow..Name)
nStaticHandle is int = GetDlgItem(nhMain, ID_STATIC)
IF nStaticHandle = 0 THEN
	nStaticHandle = API(&amp;quot;USER32&amp;quot;, &amp;quot;CreateWindowExA&amp;quot;, 0, &amp;quot;LISTBOX&amp;quot;, &amp;quot;&amp;quot;, WS_CHILD + LBS_HASSTRINGS, 0, 0, 0, 0, nhMain, ID_STATIC, Instance, 0)
END
IF nStaticHandle THEN
	nIndex is int = SendMessage(nStaticHandle, 399, -1, &amp;amp;sUseString)
	IF nIndex &amp;gt; -1 THEN // We found same static
		SendMessage(nStaticHandle, 410, nIndex, nValue)
	ELSE // Add it to our static list
		SendMessage(nStaticHandle, 410, SendMessage(nStaticHandle, 384, 0, &amp;amp;sUseString), nValue)
	END
END

GetStaticValue
FUNCTION GetStaticValue(LOCAL sUseString is string)
sUseString += &amp;quot;|&amp;quot;
nValue is int = 0
nhMain is int = Handle(MyWindow..Name)
nStaticHandle is int = GetDlgItem(nhMain, ID_STATIC)
IF nStaticHandle THEN
	nIndex is int = SendMessage(nStaticHandle, 399, -1, &amp;amp;sUseString)
	IF nIndex &amp;gt; -1 THEN // We found it
		nValue = SendMessage(nStaticHandle, 409, nIndex, 0)
	END
END
RESULT nValue

Example showing how to use it:
nFlip is int = GetStaticValue(&amp;quot;nFlip&amp;quot;); nFlip = NOT nFlip; SetStaticValue(&amp;quot;nFlip&amp;quot;, nFlip)</description>
        <link>https://www.wxforum.info/read.php?27161,30119,30119#msg-30119</link>
        <lastBuildDate>Wed, 16 Sep 2026 05:37:54 +0200</lastBuildDate>
        <generator>Phorum 5.2.23</generator>
        <item>
            <guid>https://www.wxforum.info/read.php?27161,30119,88943#msg-88943</guid>
            <title>Re: Using STATIC variables within a procedure</title>
            <link>https://www.wxforum.info/read.php?27161,30119,88943#msg-88943</link>
            <description><![CDATA[ [<a href="http://en.wikipedia.org/wiki/Global_variable"  rel="nofollow">en.wikipedia.org</a>]<br />
<br />
[<a href="http://en.wikipedia.org/wiki/Static_variable"  rel="nofollow">en.wikipedia.org</a>]<br />
<br />
...<br />
<br />
<br />
<br />
<br />
<br />
<br />
]]></description>
            <dc:creator>Patrice Terrier</dc:creator>
            <category>WinDev Solutions</category>
            <pubDate>Mon, 27 Dec 2010 09:24:49 +0100</pubDate>
        </item>
        <item>
            <guid>https://www.wxforum.info/read.php?27161,30119,88942#msg-88942</guid>
            <title>Re: Using STATIC variables within a procedure</title>
            <link>https://www.wxforum.info/read.php?27161,30119,88942#msg-88942</link>
            <description><![CDATA[ <br />
Hi Patrice,<br />
<br />
I do understand your wish - but it&#039;s against WinDev&#039;s logic!<br />
<br />
In WinDev, you can abort any procedure-in-procedure and directly return to the originally calling code without leaving traces, means without building up a stack. Seems, if the instruction pointer leaves ANY procedure then all of its procedure-defined variables are destroyed automatically. Thus, the buildup of a stack with furthery unused variables is avoided. Even in WinDev, there are not many instructions which allow to abruptly end the execution of a deep series of stacked procedures - at least ReturnToCapture(..) is one of them. A &#039;STATIC&#039; variable would inhibit usage of ReturnToCapture(..) within any second level procedure.<br />
<br />
The natural workaround are global variables, for global procedures in project code and for local procedures in window code. I can&#039;t see what&#039;s so bad in using global variables ?<br />
<br />
Kind regards,<br />
Guenter<br />
]]></description>
            <dc:creator>Jimbo</dc:creator>
            <category>WinDev Solutions</category>
            <pubDate>Mon, 27 Dec 2010 07:30:43 +0100</pubDate>
        </item>
        <item>
            <guid>https://www.wxforum.info/read.php?27161,30119,87476#msg-87476</guid>
            <title>Re: Using STATIC variables within a procedure</title>
            <link>https://www.wxforum.info/read.php?27161,30119,87476#msg-87476</link>
            <description><![CDATA[ No, because the LoadParameter/SaveParameter clutters the registry, and there is no need to keep the values between sessions.<br />
<br />
Another solution is to use HeapAlloc/HeapReaAlloc/HeapFree, however more complex to program because it requires a good knowledge of the core API.<br />
<br />
...<br />
]]></description>
            <dc:creator>Patrice Terrier</dc:creator>
            <category>WinDev Solutions</category>
            <pubDate>Fri, 10 Dec 2010 13:45:33 +0100</pubDate>
        </item>
        <item>
            <guid>https://www.wxforum.info/read.php?27161,30119,87474#msg-87474</guid>
            <title>Re: Using STATIC variables within a procedure</title>
            <link>https://www.wxforum.info/read.php?27161,30119,87474#msg-87474</link>
            <description><![CDATA[ Hello<br />
<br />
I know what static means, but solution i&#039;ve offered you can do this just replace your two functions &quot;GetStaticValue&quot; and &quot;SetStaticValue&quot; and you will achieve the same result<br />
<br />
Regards,<br />
Hristo Dimitrov]]></description>
            <dc:creator>Hristo Dimitrov</dc:creator>
            <category>WinDev Solutions</category>
            <pubDate>Fri, 10 Dec 2010 12:59:03 +0100</pubDate>
        </item>
        <item>
            <guid>https://www.wxforum.info/read.php?27161,30119,87470#msg-87470</guid>
            <title>Re: Using STATIC variables within a procedure</title>
            <link>https://www.wxforum.info/read.php?27161,30119,87470#msg-87470</link>
            <description><![CDATA[ I am speaking of STATIC variable as they could be used with C++, within procedures and functions.<br />
<br />
In C++, when you declare a variable in a function, <b>the static keyword specifies that the variable retains its state between calls to that function</b>.<br />
<br />
This has nothing to do with storing/retrieving values in/from an INI file.<br />
<br />
[<a href="http://msdn.microsoft.com/en-us/library/s1sb61xd.aspx"  rel="nofollow">msdn.microsoft.com</a>]<br />
<br />
...<br />
<br />
<br />
<br />
]]></description>
            <dc:creator>Patrice Terrier</dc:creator>
            <category>WinDev Solutions</category>
            <pubDate>Fri, 10 Dec 2010 12:26:25 +0100</pubDate>
        </item>
        <item>
            <guid>https://www.wxforum.info/read.php?27161,30119,87467#msg-87467</guid>
            <title>Re: Using STATIC variables within a procedure</title>
            <link>https://www.wxforum.info/read.php?27161,30119,87467#msg-87467</link>
            <description><![CDATA[ Hello<br />
<br />
Other solution beside ini files is to use &quot;SaveParameter&quot;, &quot;LoadParameter&quot;, &quot;InitParameter&quot; but they can only operate on windows, that&#039;s why i suggested you to use ini files.<br />
<br />
Regards<br />
Hristo Dimitrov]]></description>
            <dc:creator>Hristo Dimitrov</dc:creator>
            <category>WinDev Solutions</category>
            <pubDate>Fri, 10 Dec 2010 12:06:34 +0100</pubDate>
        </item>
        <item>
            <guid>https://www.wxforum.info/read.php?27161,30119,87466#msg-87466</guid>
            <title>Re: Using STATIC variables within a procedure</title>
            <link>https://www.wxforum.info/read.php?27161,30119,87466#msg-87466</link>
            <description><![CDATA[ Hello<br />
<br />
This understanding about static variables comes from VB, no other language has the same meaning about STATIC : C#, java, etc.<br />
<br />
So to say that C#, java and WD are against good programming practices is ....<br />
<br />
better solution is to use classes or if you are like procedure use ini files to hold your variables ;)<br />
<br />
<br />
Regards,<br />
Hristo Dimitrov]]></description>
            <dc:creator>Hristo Dimitrov</dc:creator>
            <category>WinDev Solutions</category>
            <pubDate>Fri, 10 Dec 2010 12:02:14 +0100</pubDate>
        </item>
        <item>
            <guid>https://www.wxforum.info/read.php?27161,30119,30119#msg-30119</guid>
            <title>Using STATIC variables within a procedure</title>
            <link>https://www.wxforum.info/read.php?27161,30119,30119#msg-30119</link>
            <description><![CDATA[ There is one big feature i am missing in WinDev:<br />
<span style="color:#FF3366">the use of <b>STATIC</b> variables in a procedure to preserve specific values from one call to another.</span><br />
<br />
Actualy there is no other alternative than using GLOBALs, however this is considered as a bad programming pratice in most languages i know, and it doesn&#039;t help to create modular procedures that can be reused from one project to another.<br />
<br />
To work around this problem, i have written a couple of functions (see the code below), <b>but if you have a better solution i would be glad to know about it.</b><br />
<br />
Thank you!<br />
<br />
<br />
_________________________________________________________________<br />
<br />
<span style="color:#0000FF"><b>SetStaticValue</b><pre class="bbcode">
PROCEDURE SetStaticValue(LOCAL sUseString is string, LOCAL nValue is int)
sUseString += &quot;|&quot;
nhMain is int = Handle(MyWindow..Name)
nStaticHandle is int = GetDlgItem(nhMain, ID_STATIC)
IF nStaticHandle = 0 THEN
	nStaticHandle = API(&quot;USER32&quot;, &quot;CreateWindowExA&quot;, 0, &quot;LISTBOX&quot;, &quot;&quot;, WS_CHILD + LBS_HASSTRINGS, 0, 0, 0, 0, nhMain, ID_STATIC, Instance, 0)
END
IF nStaticHandle THEN
	nIndex is int = SendMessage(nStaticHandle, 399, -1, &amp;sUseString)
	IF nIndex &gt; -1 THEN // We found same static
		SendMessage(nStaticHandle, 410, nIndex, nValue)
	ELSE // Add it to our static list
		SendMessage(nStaticHandle, 410, SendMessage(nStaticHandle, 384, 0, &amp;sUseString), nValue)
	END
END</pre>
<br />
<b>GetStaticValue</b><pre class="bbcode">
FUNCTION GetStaticValue(LOCAL sUseString is string)
sUseString += &quot;|&quot;
nValue is int = 0
nhMain is int = Handle(MyWindow..Name)
nStaticHandle is int = GetDlgItem(nhMain, ID_STATIC)
IF nStaticHandle THEN
	nIndex is int = SendMessage(nStaticHandle, 399, -1, &amp;sUseString)
	IF nIndex &gt; -1 THEN // We found it
		nValue = SendMessage(nStaticHandle, 409, nIndex, 0)
	END
END
RESULT nValue</pre></span><br />
<br />
Example showing how to use it:<br />
<span style="color:#0000FF">nFlip is int = <b>GetStaticValue</b>(&quot;nFlip&quot;); nFlip = NOT nFlip; <b>SetStaticValue</b>(&quot;nFlip&quot;, nFlip)</span>]]></description>
            <dc:creator>Patrice Terrier</dc:creator>
            <category>WinDev Solutions</category>
            <pubDate>Wed, 24 Jun 2009 15:19:41 +0200</pubDate>
        </item>
    </channel>
</rss>
