Patrice Terrier
Using STATIC variables within a procedure
June 24, 2009 03:19PM
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'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 += "|"
nhMain is int = Handle(MyWindow..Name)
nStaticHandle is int = GetDlgItem(nhMain, ID_STATIC)
IF nStaticHandle = 0 THEN
	nStaticHandle = API("USER32", "CreateWindowExA", 0, "LISTBOX", "", WS_CHILD + LBS_HASSTRINGS, 0, 0, 0, 0, nhMain, ID_STATIC, Instance, 0)
END
IF nStaticHandle THEN
	nIndex is int = SendMessage(nStaticHandle, 399, -1, &sUseString)
	IF nIndex > -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, &sUseString), nValue)
	END
END

GetStaticValue
FUNCTION GetStaticValue(LOCAL sUseString is string)
sUseString += "|"
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, &sUseString)
	IF nIndex > -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("nFlip"); nFlip = NOT nFlip; SetStaticValue("nFlip", nFlip)
Hristo Dimitrov
Re: Using STATIC variables within a procedure
December 10, 2010 12:02PM
Hello

This understanding about static variables comes from VB, no other language has the same meaning about STATIC : C#, java, etc.

So to say that C#, java and WD are against good programming practices is ....

better solution is to use classes or if you are like procedure use ini files to hold your variables winking smiley


Regards,
Hristo Dimitrov
Hristo Dimitrov
Re: Using STATIC variables within a procedure
December 10, 2010 12:06PM
Hello

Other solution beside ini files is to use "SaveParameter", "LoadParameter", "InitParameter" but they can only operate on windows, that's why i suggested you to use ini files.

Regards
Hristo Dimitrov
Patrice Terrier
Re: Using STATIC variables within a procedure
December 10, 2010 12:26PM
I am speaking of STATIC variable as they could be used with C++, within procedures and functions.

In C++, when you declare a variable in a function, the static keyword specifies that the variable retains its state between calls to that function.

This has nothing to do with storing/retrieving values in/from an INI file.

[msdn.microsoft.com]

...







Edited 1 time(s). Last edit at 12/10/2010 12:28PM by Patrice Terrier.
Hristo Dimitrov
Re: Using STATIC variables within a procedure
December 10, 2010 12:59PM
Hello

I know what static means, but solution i've offered you can do this just replace your two functions "GetStaticValue" and "SetStaticValue" and you will achieve the same result

Regards,
Hristo Dimitrov
Patrice Terrier
Re: Using STATIC variables within a procedure
December 10, 2010 01:45PM
No, because the LoadParameter/SaveParameter clutters the registry, and there is no need to keep the values between sessions.

Another solution is to use HeapAlloc/HeapReaAlloc/HeapFree, however more complex to program because it requires a good knowledge of the core API.

...
Jimbo
Re: Using STATIC variables within a procedure
December 27, 2010 07:30AM

Hi Patrice,

I do understand your wish - but it's against WinDev's logic!

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 'STATIC' variable would inhibit usage of ReturnToCapture(..) within any second level procedure.

The natural workaround are global variables, for global procedures in project code and for local procedures in window code. I can't see what's so bad in using global variables ?

Kind regards,
Guenter
Patrice Terrier
Re: Using STATIC variables within a procedure
December 27, 2010 09:24AM
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: