<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to 'VAR_STAT' declaration not allowed in this place - bug?</title><link href="https://forge.codesys.com/forge/talk/Runtime/thread/4b31617666/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/Runtime/thread/4b31617666/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/Runtime/thread/4b31617666/</id><updated>2020-05-04T15:59:23.367000Z</updated><subtitle>Recent posts to 'VAR_STAT' declaration not allowed in this place - bug?</subtitle><entry><title>'VAR_STAT' declaration not allowed in this place - bug?</title><link href="https://forge.codesys.com/forge/talk/Runtime/thread/4b31617666/?limit=25#12f7" rel="alternate"/><published>2020-05-04T15:59:23.367000Z</published><updated>2020-05-04T15:59:23.367000Z</updated><author><name>angel</name><uri>https://forge.codesys.com/u/angel/</uri></author><id>https://forge.codesys.com0b7167a074bf5ef336d2717523f28b49fc5f2651</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Maybe you are using a Program instead a Function. &lt;br/&gt;
I had the same problem using a POU, and not using a Function&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>'VAR_STAT' declaration not allowed in this place - bug?</title><link href="https://forge.codesys.com/forge/talk/Runtime/thread/4b31617666/?limit=25#9597" rel="alternate"/><published>2020-02-27T15:40:03.738000Z</published><updated>2020-02-27T15:40:03.738000Z</updated><author><name>McFeardon</name><uri>https://forge.codesys.com/u/mcfeardon/</uri></author><id>https://forge.codesys.com70bb6b9bd21e020749c18420fe99169254d1d144</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Volvo: I am using static variables in Codesys 3.5.7 without any issues.&lt;/p&gt;
&lt;p&gt;Scott: Static variables are generally used to share a common data between multiple instances of the same object/function block.&lt;/p&gt;
&lt;p&gt;For example, if you were to declare:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;FUNCTION_BLOCK&lt;/span&gt; &lt;span class="k"&gt;PUBLIC&lt;/span&gt; &lt;span class="n"&gt;MyObject&lt;/span&gt;
&lt;span class="n"&gt;VAR_STATIC&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="n"&gt;iCount&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;END_VAR&lt;/span&gt;
&lt;span class="n"&gt;____________________&lt;/span&gt;
&lt;span class="n"&gt;iCount&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;iCount&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Then in your main program:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;VAR&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="n"&gt;oMyObject1&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MyObject&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;initializes&lt;/span&gt; &lt;span class="n"&gt;iCount&lt;/span&gt; &lt;span class="k"&gt;to&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="n"&gt;oMyObject2&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;MyObject&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;iCount&lt;/span&gt; &lt;span class="n"&gt;already&lt;/span&gt; &lt;span class="n"&gt;initialized&lt;/span&gt;
&lt;span class="n"&gt;END_VAR&lt;/span&gt;
&lt;span class="n"&gt;____________________&lt;/span&gt;
&lt;span class="n"&gt;oMyObject1&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;iCount&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="n"&gt;equal&lt;/span&gt; &lt;span class="k"&gt;to&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="n"&gt;oMyObject2&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt; &lt;span class="err"&gt; &lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;iCount&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="n"&gt;equal&lt;/span&gt; &lt;span class="k"&gt;to&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="n"&gt;oMyObject1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;iCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;oMyObject2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;iCount&lt;/span&gt; &lt;span class="k"&gt;at&lt;/span&gt; &lt;span class="k"&gt;all&lt;/span&gt; &lt;span class="n"&gt;times&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;I haven't found a good resource on static variables in Codesys, but the following link is a reference to static variables in Java. The same concepts still apply.&lt;br/&gt;
&lt;a class="" href="https://www.caveofprogramming.com/java/java-for-beginners-static-variables-what-are-they.html" rel="nofollow"&gt;https://www.caveofprogramming.com/java/ ... -they.html&lt;/a&gt; m &lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>'VAR_STAT' declaration not allowed in this place - bug?</title><link href="https://forge.codesys.com/forge/talk/Runtime/thread/4b31617666/?limit=25#32d1" rel="alternate"/><published>2020-02-27T15:40:01.725000Z</published><updated>2020-02-27T15:40:01.725000Z</updated><author><name>Anonymous</name><uri>https://forge.codesys.com/u/*anonymous/</uri></author><id>https://forge.codesys.com2de4fa8801694f818f46dc6e2e26685f5bdaf035</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;&lt;em&gt;Originally created by:&lt;/em&gt; scott_cunningham&lt;/p&gt;
&lt;p&gt;From the online help:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Zitat:&lt;/strong&gt;&lt;br/&gt;
This feature is an extension to the IEC 61131-3 standard.&lt;br/&gt;
Static variables can be used in function blocks, methods and functions. They have to be declared locally between the keywords VAR_STAT and END_VAR and will be initialized at the first call of the respective POU. &lt;br/&gt;
Static variables are only accessible within the scope in which they are declared (like a static variable in C), but like a global variable do not loose its value after the POU is left. For example in a function they might be used as a counter for the number of function calls.&lt;br/&gt;
Regard the possibility of adding an attribute to 'VAR_STAT'.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In reality, it is not necessary for function blocks, since regular VARs also keep their value from call to call..  Program VARs also hold their value from call to call.&lt;/p&gt;
&lt;p&gt;I've never had the need to use them.  If I need some type of counter or "memory" in a method, I define the variable as a VAR in the function block the method is attached to.  I only use functions for conversions of sorts, so no need to keep a value from call to call.  I suppose in the help's example one may want to know how many instances of a function block exist... but in general, you already know that.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>'VAR_STAT' declaration not allowed in this place - bug?</title><link href="https://forge.codesys.com/forge/talk/Runtime/thread/4b31617666/?limit=25#fe54" rel="alternate"/><published>2020-02-27T15:40:01.019000Z</published><updated>2020-02-27T15:40:01.019000Z</updated><author><name>Volvo742</name><uri>https://forge.codesys.com/u/volvo742/</uri></author><id>https://forge.codesys.comd58eb1ec2195ca16a2adf3cebc79ba3504392a75</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hello.&lt;/p&gt;
&lt;p&gt;When i createing a new variable i have the possibility to choose that the variable should be declared as a static variable.&lt;/p&gt;
&lt;p&gt;When i do that i got that error that i can't declare static variable in the declaration area. &lt;/p&gt;
&lt;p&gt;Is that a bug in Codesys? &lt;/p&gt;
&lt;p&gt;I use Codesys 3.5 patch 4.&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>