How to Access a Variable Across Two Files

How can I access and modify a global variable across multiple files?

In C, global variables are global to the execution unit (process), not global to the execution environment (system). Standard C does not provide a mechanism to share memory between processes. However, your platform (Linux, Windows, MacOS, etc.) probably does. Look up "shared memory".

How do I share a global variable between c files?

file 1:

int x = 50;

file 2:

extern int x;

printf("%d", x);

How do I share variables between different .c files?

In fileA.c:

int myGlobal = 0;

In fileA.h

extern int myGlobal;

In fileB.c:

#include "fileA.h"
myGlobal = 1;

So this is how it works:

  • the variable lives in fileA.c
  • fileA.h tells the world that it exists, and what its type is (int)
  • fileB.c includes fileA.h so that the compiler knows about myGlobal before fileB.c tries to use it.

How do I share a global variable between multiple files?

If you truly want a global variable (not advisable, of course) then you're always 100% free to do

window.globalVar = 0;

in any of your modules.


The more robust solution would of course be to have this global variable sit in some sort of dedicated module, like

globalVar.js

export default {
value: 0
};

and then you could

import globalVal from './globalVar';

globalVal.value = 'whatever';

from any modules needed.

The only risk would be that webpack might duplicate this same "global" value into multiple bundles if you're code splitting, depending on your setup. So separate module would be using their own local copy of this not-so-global variable. EDIT - this isn't true. webpack never did this; that comment was based on a misunderstanding on my part.

Using global variables between files?

The problem is you defined myList from main.py, but subfile.py needs to use it. Here is a clean way to solve this problem: move all globals to a file, I call this file settings.py. This file is responsible for defining globals and initializing them:

# settings.py

def init():
global myList
myList = []

Next, your subfile can import globals:

# subfile.py

import settings

def stuff():
settings.myList.append('hey')

Note that subfile does not call init()— that task belongs to main.py:

# main.py

import settings
import subfile

settings.init() # Call only once
subfile.stuff() # Do stuff with global var
print settings.myList[0] # Check the result

This way, you achieve your objective while avoid initializing global variables more than once.

Access a variable across multiple script file under a GAS project

  • You want to use userProperties with the value, which is given in the function of onEdit, at the function of onEditOfH3, when onEdit is run.
  • You want to understand about the specification of the files in one GAS project.

If my understanding is correct, how about this answer? Please think of this as just one of several answers.

About all files in one GAS project:

At the project of Google Apps Script, all files in the project are used as one project. Namely, for example, when the following sample script is run in the file of Code.gs,

function myFunction() {
for (var i in this) {
if (typeof this[i] == "function") {
Logger.log(i)
}
}
}

all functions in all files in the project are returned. From this, it is found that when a global variable is declared at the file of Code.gs, this variable can be used at other file in the same project. At the following pattern 1, this is used.

Pattern 1:

In this pattern, userProperties is declared as the global variable.

Modified script:

Code.gs

var userProperties; // This is declared as the global variable.

function onEdit(e){
totalHoursLoggedForStory_today = Number(e.range.getSheet().getRange("H3").getValue()) + Number(e.range.getSheet().getRange("H4").getValue()) + Number(e.range.getSheet().getRange("H5").getValue());

userProperties = PropertiesService.getUserProperties(); // Modified
var newProperties = {'hoursLogged': totalHoursLoggedForStory_today };
userProperties.setProperties(newProperties);

onEditOfH3(e);
}
23546.gs

This is not required to be modified.

Pattern 2:

In this pattern, userProperties is added to the object of e. And the value is used as e.userProperties in the function of onEditOfH3. If you don't want to use the global variable, how about this? Also, userProperties can be sent as another argument.

Modified script:

Code.gs

function onEdit(e){
totalHoursLoggedForStory_today = Number(e.range.getSheet().getRange("H3").getValue()) + Number(e.range.getSheet().getRange("H4").getValue()) + Number(e.range.getSheet().getRange("H5").getValue());

var userProperties = PropertiesService.getUserProperties();
var newProperties = {'hoursLogged': totalHoursLoggedForStory_today };
userProperties.setProperties(newProperties);
e.userProperties = userProperties; // Added
onEditOfH3(e);
}
23546.gs

function onEditOfH3(e){
// var sh = SpreadsheetApp.getUi(); // In your whole script, this might be declared at elsewhere.
if (e.range.getA1Notation() == "H3") {
var temp = e.userProperties.getProperty('hoursLogged'); // Modified
sh.alert(temp);
}
}

Reference:

  • Script Projects

Using the same variable across multiple files in C++

Define the variable in one file like:

type var_name;

And declare it global in the other file like:

extern type var_name;


Related Topics



Leave a reply



Submit