static-variables

Are static local variables bad practice?

孤人 提交于 2019-12-19 17:45:24
问题 Related C++ question: Static local variables in methods a bad practice? In VB.NET, when I want a simple counter or something that increments each time a method is called, I often find myself writing code like: Private Sub tmrRefresh_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrRefresh.Tick Static a As Integer = 0 a += 1 '...rest of method depends on a End Sub Is this recommended in VB.NET and OOP in general? 回答1: Are static local variables bad practice? No .

What exactly does “static” mean when declaring “global” variables in Java?

瘦欲@ 提交于 2019-12-19 17:39:57
问题 I've been running into this problem many times and I never bothered to learn why its happening and learn what "static" actually means. I just applied the change that Eclipse suggested and moved on. public class Member { // Global Variables int iNumVertices; int iNumEdges; public static void main(String[] args) { // do stuff iNumVertices = 0; // Cannot make a static reference to the non-static field iNumVertices // do more stuff } // main end } So eclipse tells me to do static int iNumVertices

Is a static member variable common for all C# generic instantiations?

◇◆丶佛笑我妖孽 提交于 2019-12-19 12:49:13
问题 In C# I have a generic class: public class MyGeneric<ParameterClass> where ParameterClass: MyGenericParameterClass, new() { public static int Variable; } Now in C++ if I instantiated a templated class with different parameters each complete class would get it's own Variable , so I just can't say MyGeneric.Variable = 1; // invalid in C++ in C++, but seems like I can do so in C#. I'd like to clarify... If I have a generic with a static member variable is that variable shared among all generic

static variable initialisation code never gets called

拟墨画扇 提交于 2019-12-18 09:24:45
问题 I've got an application that's using a static library I made. One .cpp file in the library has a static variable declaration, whose ctor calls a function on a singleton that does something- e.g. adds a string. Now when I use that library from the application, my singleton doesn't seem to contain any traces of the string that was supposed to be added. I'm definitely missing something but I don't know what.. 回答1: If you have an object in a static library that is not EXPLICITLY used in the

Can I free() static and automatic variables in C?

ぃ、小莉子 提交于 2019-12-17 20:49:47
问题 The code is as follow : #include <stdlib.h> int num = 3; // Static external variable int *ptr = &num; int main(void) { int num2 = 4; // Automatic variable int *ptr2 = &num2; free(ptr); //Free static variable free(ptr2); //Free automatic variable return 0; } I try to compile the above code and it works, I'm curious does the free() function able to free both the static variable and also automatic variable? Or basically it does nothing? 回答1: Calling free() on a pointer not returned by memory

What makes a static variable initialize only once?

六月ゝ 毕业季﹏ 提交于 2019-12-17 06:07:03
问题 I noticed that if you initialize a static variable in C++ in code, the initialization only runs the first time you run the function. That is cool, but how is that implemented? Does it translate to some kind of twisted if statement? (if given a value, then ..) void go( int x ) { static int j = x ; cout << ++j << endl ; // see 6, 7, 8 } int main() { go( 5 ) ; go( 5 ) ; go( 5 ) ; } 回答1: Yes, it does normally translate into an implicit if statement with an internal boolean flag. So, in the most

What makes a static variable initialize only once?

寵の児 提交于 2019-12-17 06:06:13
问题 I noticed that if you initialize a static variable in C++ in code, the initialization only runs the first time you run the function. That is cool, but how is that implemented? Does it translate to some kind of twisted if statement? (if given a value, then ..) void go( int x ) { static int j = x ; cout << ++j << endl ; // see 6, 7, 8 } int main() { go( 5 ) ; go( 5 ) ; go( 5 ) ; } 回答1: Yes, it does normally translate into an implicit if statement with an internal boolean flag. So, in the most

Reset Static variables in IIS

≡放荡痞女 提交于 2019-12-13 16:24:48
问题 I have assigned value for static variable in Application start event of ASP.NET MVC application. protected void Application_Start() { public static list<string> versionInfo = VersionAccess.LoadVersionInfo(); } In this static variable "versionInfo" assigned list of some values that get from database. Used this static variable throughout the mvc application. This is working fine when i hosted this app in IIS. And later i have added some more values in database. So the static variable values

Are static variable truly “global” (system-wide) in Android?

为君一笑 提交于 2019-12-13 13:39:40
问题 A quick note at the very beginning to avoid false duplicates: There are tons of questions here about when static variables get cleared and how long they live. This is not what I'm asking about here. If I have a static variable in a program on a PC and I launch two different copies of the program, then each copy will usually run in its own sandbox with its own private values for its static variables. So, they are not system-wide global (not sure if that is good terminology here). Are there

How am I able to access a static variable from another file? [duplicate]

那年仲夏 提交于 2019-12-13 04:44:37
问题 This question already has answers here : In C, how do I restrict the scope of a global variable to the file in which it's declared? (4 answers) Closed 5 years ago . How am I able to access a static variable from another file? Doesn't static variable have a file scope? bash-3.2$ ls a.c b.c bash-3.2$ cat a.c #include <stdio.h> static int s = 100; int fn() { /* some code */ } bash-3.2$ cat b.c #include <stdio.h> #include "a.c" extern int s; int main() { printf("s = %d \n",s); return 0; } bash-3