global-variables

Definition/Initialization of Global Variables: Common Practice? (Geared towards C#)

I kind of posted a similar question a couple of days ago but that was more geared towards any *.Designer.cs file. This question is geared towards declaration and initialization of global variables within a class. To my knowledge, it's almost common practice (aside from the *.Designer.cs files it seems) to place all global variables at ...

How do I avoid both global variables and magic numbers?

I know and understand that global variables and magic numbers are things to avoid when programming, particularly as the amount of code in your project grows. I however can't think of a good way to go about avoiding both. Say I have a pre-determined variable representing the screen width, and the value is needed in multiple files. I coul...

Global variables in Windows.Forms

Hi all, This may seem like a dumb topic, but I'm trying to learn some good coding practices. I'm making a windows.forms application and I have reached a point where my partial Form class has 7 global variables (and their corresponding properties) declared and used - to name a few: one to determine if the app is registered, a Settings o...

In a C program, is it possible to reset all global variables to default vaues?

I have a legacy C Linux application that I need to reuse . This application uses a lot of global variables. I want to reuse this application's main method and invoke that in a loop. I have found that when I call the main method( renamed to callableMain) in a loop , the application behavior is not consistent as the values of global varia...

Can I prevent modifying an object in Python?

I want to control global variables (or globally scoped variables) in a way that they are set only once in program initialization code, and lock them after that. I use UPPER_CASE_VARIABLES for global variables, but I want to have a sure way not to change the variable anyway. Does python provide that (or similar) feature? How do you c...

Why does the function return the value of the local JSON variable instead the global?

Hello, I have a global JSON variable where I store some parameters and then each time I run the function I want to be able to modify them but just locally inside that function. So every time I run the function I want a fresh copy of the global variable inside the local one. The problem is that I copy the global variable to a local one...

Good Idea or Bad Idea? Using a Static Class Variable to Store a Global

I have a class that I am using all over the place in my code. It contains settings and other core functionality. Here's what I'm doing right now to use the class. $settings = new Settings(); $settings->loadSettings(); Then, when I need to the code in some random function in another class, I do something like this: function abc() { ...

Should functions be declared with a minimal scope?

In my experience, most JS functions, constructor functions, or module-pattern functions, are declared as globals or are bound to a global namespace object. In any case, these identifiers are globally accessible. From any line of code in your system you can have: mod1 = somepackage.SomeModule(); // couple to me! Here's a quick JS exa...

Regular class global variable being shared between object instances - help!

So I have just started learning objective c having a little Java experience and this site has been really helpful for answering loads of my questions, but I've hit an issue I can't quite fathom. Theres a fair amount to be read on the topic but I can't quite find where I've gone wrong. So I read this: Objective C does not support cl...

javascript variable access mystery

var foo = 'hello'; var myfunc = function() { console.log(foo); var foo = foo || 'world'; console.log(foo); } myfunc(); why is the first foo logged to be 'undefined' ? ...

Global Variables in Qt

Hi I want to make a global variable in Qt. So I wrote a singleton class. But I am getting the following errors :: error: symbol(s) not found, :: error: collect2: ld returned 1 exit status I am using the following code : CityBookGlobalVariables.h: class CityBookGlobalVariables { private: CityBookGlobalVariables(); CityBookGlo...

How to use acast (reshape2) within a function in R?

I tried to use acast from reshape2 within a self written function, but had the problem that acast did not find the data I send to it. Here is my data: library("reshape2") x <- data.frame(1:3, rnorm(3), rnorm(3), rnorm(3)) colnames(x) <- c("id", "var1", "var2", "var3") y <-melt(x, id = "id", measure = c("var1", "var2", "var3")) y ...

Making all variables in a scope global or importing a module inside another module

I have a package with two modules in it. One is the __init__ file, and the other is a separate part of the package. If I try from mypackage import separatepart, the code in the __init__ module is run, which will run unneeded code, slowing down the importing by a lot. The code in separate part won't cause any errors, and so users should b...

VBA global variables, multiple workbooks

I have a VB application, that uses some global variables to store data that is required by multiple forms and modules, this works fine. However if a user opens up another workbook, running the same VBA application, then they end up accessing (and changing) the same public variables. How can I have workbook level global variables, or if ...

Accessing global variable in "CSS" (style.php)

I'm doing a style.php CSS file so I can use some dynamic variables in the CSS within a Wordpress installation: <?php header("Content-type: text/css"); ?> and so on. How can I access a global variable from within the style.php file or pass a variable to it? The code I'm trying to get to work within the CSS is like $maincolor = $cap-...

Sharing data between selected users in web application c#

Hi, My website uses a web user control. The properties for the user control will be set will be common for a set of my users. e.g. if I have 20 users accessing my website, 5 of them may be using the user control with id = 1 , 4 using the user control with id =2. I have a property associated with each user control which I would like to b...

Another static initialization order problem in C++

This is another variation of an old theme: The initialization order of static objects in different translation units is not defined. Below is a stripped-down example of my particular szenario. The classes G and F are non-POD types. F depends on G is the sense that to construct an instance of F you need some number of instances of G. (Fo...

Global variables in C++

I am working with some C++ code that has a timer and the timer runs this: char buf[1024]; ZeroMemory(&buf, sizeof(buf)); somefunction(buf); // this put stuff into buf otherfunction(buf); // this do stuff with buf somefunction() does a web request and InternetReadFile() puts the data in "buf" But I need to be able to read the previous...

Haxe for javascript without global namespace pollution?

I've known about haxe for a while, but never really played with it until yesterday. Being curious, I decided to port showdown.js, a javascript port of markdown.pl, to haxe. This was pretty straightforward, and the javascript it generates seems to run fine (edit: If you want to see it in action, check it out here). However, I noticed tha...

Avoiding pollution of globals via iframe script loader?

Problem... Poorly-coded scripts exist which need to be included on a web page. These scripts pollute the global scope by doing things like: Assigning values to undeclared identifiers Adding properties to built-in constructor functions (like Object and Array) and their prototypes Other nasty stuff. Solution? I want to include t...