Welcome! Log In Create A New Profile

Advanced

Using STATIC variables within a procedure

Posted by Patrice Terrier 
Patrice Terrier
Using STATIC variables within a procedure
June 18, 2009 10:16AM
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)
Al
Re: Using STATIC variables within a procedure
June 18, 2009 11:36AM
Hello Patrice

Thanks for the contribution to the forum, much appreciated.

Regards
Al
Patrice Terrier
Re: Using STATIC variables within a procedure
June 18, 2009 12:17PM
Forget to say that ID_STATIC must be a unique ID constant.

For example:
CONSTANT
          ID_CTRL1 = -1
          ID_CLOCK = -11
          ID_STATIC = -12	
          ID_BACKGROUND = -999
END
Michel Fages
Re: Using STATIC variables within a procedure
June 18, 2009 01:12PM
Hi all,

Just for fun, here is the method I've been using for years (a little inconvenience = the "note" information of the window should not be filled in the editor) :

PROCEDURE SetStaticvalue(LOCAL sUseString is string, LOCAL sValue is string)

as_values is dynamic array of 0 by 2 strings
b_buffer is Buffer = MyWindow..Note

IF MyWindow..Note<>"" THEN
        Deserialize(as_values, b_buffer, psdXML)
END
ArrayAddLine(as_values, sUseString, sValue)
ArraySort(as_values, asAscending, 1)

Serialize(as_values, b_buffer, psdXML)
MyWindow..Note = b_buffer



PROCEDURE GetStaticValue(LOCAL sUseString is string)

as_values is dynamic array of 0 by 2 strings
b_buffer is Buffer = MyWindow..Note
le_ind is int

IF MyWindow..Note<>"" THEN
        Deserialize(as_values, b_buffer, psdXML)
END

le_ind =  ArraySeek(as_values, asBinary, 1, sUseString)
IF le_ind>0 THEN
        RESULT as_values[le_ind][2]
ELSE
        RESULT ""
END


Regards,

Michel Fages
[www.hexo7.com]
Fabrice Harari
Re: Using STATIC variables within a procedure
June 18, 2009 01:14PM
Hi Patrice...

it looks to me like OOP is your solution

Best regards

Patrice Terrier
Re: Using STATIC variables within a procedure
June 18, 2009 02:05PM
Fabrice

I am using OOP only when i have no other solution, or when i am programming with a true compiler. I avoid using extra encapsulation of the core API when i can do it directly with a standard procedural call.

Indeed, if R&D people from PC-Soft are reading this thread, it would be very nice if they could sacrifice some new extra gadgets in favor of built-in STATIC support winking smiley

...
Patrice Terrier
Re: Using STATIC variables within a procedure
June 18, 2009 02:27PM
Michel

MyWindow..Note, string manipulation is a slow process when you need to use STATIC variable in a real time process (I would like to use it for animation purpose).

Usualy STATIC variable are saved on the stack, we could do something similar, but this would require the writting of a small Win32.dll ...




Michel Fages
Re: Using STATIC variables within a procedure
June 18, 2009 02:35PM
Hello Patrice,

You are right, real static variables saved on the stack would be much faster than string manipulation.

I do not know if using list fields+SendMessage (processed by Windows' events system) is faster or not than accessing the Windows..Note property. When I've got time I will check this.

Regards,

Michel Fages
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: