Static initialisieren

Previous topic - Next topic

WPShadow

Hi!

Stehe mal wieder mit meinem Latein vor einer Mauer:

Ich möchte in einer Funktion einen Static - Wert quasi als Default initialisieren, aber des weiteren umändern. Dabei wird die Funktion immer wieder aufgerufen. Hat da jemand eine Idee für mich?

Per Code sieht das inetwa so aus:

Code (glbasic) Select


WHILE TRUE
    bla()
WEND

FUNCTION bla:
STATIC hui

    hui = Veränderung

ENDFUNCTION



Oder zumindest so ähnlich!

GLB

W.
AMD X2 4600, 2 GB Ram, ATI X1950 XTX, XP PRO SP2: GLB Premium 10.beta_dingsi, <(´.´<) Kirby Dance (>`.`)>
http://lostrevenant.blogspot.com
alea iacta est

D2O

Hmm, ich hoffe ich hab das verstanden.


Code (glbasic) Select
WHILE TRUE
    bla(0)
WEND




FUNCTION bla:pos = 0
STATIC hui AS m


  RETURN hui.a[pos] = veraenderung

ENDFUNCTION

TYPE m
a[10]
ENDTYPE
I7 2600K; 8 GB RAM ; Win10 Pro x64 | NVidia GTX 750 TI 2048MB ; Realtec OnBoard Sound;
Lenovo ThinkPad T400: XP Pro
GLB Premium-immer Aktuell

Moru

#2
If you want to initialize the value on first run, make a new static that you never change in the code so when the routine starts you can be sure that the value will be 0 on first run. Then init the variable you want and then set first_time to 1 or whatever.

Code (glbasic) Select

WHILE TRUE
    bla()
WEND

FUNCTION bla:
    STATIC hui, first_time

    if first_time = 0
        hui = 42 // init
        first_time = 1
    endif

    hui = Veränderung

ENDFUNCTION

WPShadow

Great! Thank you!  =D
AMD X2 4600, 2 GB Ram, ATI X1950 XTX, XP PRO SP2: GLB Premium 10.beta_dingsi, <(´.´<) Kirby Dance (>`.`)>
http://lostrevenant.blogspot.com
alea iacta est

Kitty Hello

If you assign something to a static, then the first time it is used this value gets assigned:

Code (glbasic) Select

poo()
poo()
poo()

FUNCTION poo:
STATIC c = 5
   DEBUG c + "\n"
   INC c, 1
ENDFUNCTION


Output:
5
6
7



Moru

this only works if it is a number or string, cant init with a return value from a function or similar. So I preffer to be safe, when I was using static init the other way my program would work and then stop working on the next update :-)