# Global Variable shared between functions

**URL:** <https://community.cumulocity.com/t/global-variable-shared-between-functions/7458>\
**Category:** Forum\
**Tags:** cumulocity\
**Created:** [October 16, 2023, 12:08pm UTC](https://community.cumulocity.com/t/global-variable-shared-between-functions/7458 "2023-10-16T12:08:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabriele.chiossi](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@gabriele.chiossi](https://community.cumulocity.com/u/gabriele.chiossi)\
**Post date:** [October 16, 2023, 12:08pm UTC](https://community.cumulocity.com/t/global-variable-shared-between-functions/7458/1 "2023-10-16T12:08:44Z")

</div>

Hello all,  
how can I share a boolean flag between two functions?  
Something like this:

```auto
boolean newFlag := false;

monitor xxx(){
 action onload(){
  monitor.subscribe(Measurement.SUBSCRIBE_CHANNEL);
  on all Measurement(type=MEASUREMENT_TYPE_A) as measurement
  {
    newFlag := true;
  }
  monitor.subscribe(Measurement.SUBSCRIBE_CHANNEL);
  on all Measurement(type=MEASUREMENT_TYPE_B) as measurement
  {
    // here new flag needs to be true;
  }
 }
}

```

---

<div class="post-metadata">

**Author:** ![Harald\_Meyer](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/harald_meyer/32/1959_2.png) [@Harald\_Meyer](https://community.cumulocity.com/u/Harald_Meyer)\
**Post date:** [October 16, 2023, 1:11pm UTC](https://community.cumulocity.com/t/global-variable-shared-between-functions/7458/2 "2023-10-16T13:11:51Z")

</div>

Hi Gabriele,

there are multiple ways to achieve this, depending on what you want to do:

## Dynamic Listeners

You can start listeners inside of other listener

```auto
  on all Measurement(type=MEASUREMENT_TYPE_A) as measurement
  {
     on Measurement(type=MEASUREMENT_TYPE_B) as measurement
     {
     }
  }

```

Note that you should have logic in place to avoid starting the same listener multiple times (that’s why I removed the “all”) or to stop the listener when no longer needed using a listener condition including “and not ” or by explicitly calling quit() on the listener.

## Sending events

You can define your own events and send them between listeners:

```auto
  event FlagEvent {}
   
  monitor.subscribe("FlagChannel");
  on all Measurement(type=MEASUREMENT_TYPE_A) as measurement
  {
    send FlagEvent() to "FlagChannel";
  }

  on all Measurement(type=MEASUREMENT_TYPE_B) as measurement
  {
    on FlagEvent() {
    }
  }

```

## Global Variables

You can access variables defined outside of the listener. You have to be aware that listeners will get the value of the variable when they were created, so you cannot just have a Boolean variable. You can either define your own event or e.g. use a dictionary to keep track of the state (you can change the content of the object but not the object itself):

```auto
  dictionary<string,boolean> flags := {"flagA":false};
  on all Measurement(type=MEASUREMENT_TYPE_A) as measurement
  {
    flags["flagA"]:= true;
  }
  on all Measurement(type=MEASUREMENT_TYPE_B) as measurement
  {
    // access flags["flagA"]
  }

```

---

<div class="post-metadata">

**Author:** ![gabriele.chiossi](https://avatars.discourse-cdn.com/v4/letter/g/7ea924/32.png) [@gabriele.chiossi](https://community.cumulocity.com/u/gabriele.chiossi)\
**Post date:** [October 17, 2023, 8:45am UTC](https://community.cumulocity.com/t/global-variable-shared-between-functions/7458/3 "2023-10-17T08:45:07Z")

</div>

> [@Harald\_Meyer](#):
>
> You can start listeners inside of other listener

By using a listener inside another listener they will always get executed one after the other right?  
While, if they are separated their execution is independent correct?

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex005/user_avatar/community.cumulocity.com/system/32/31990_2.png) [@system](https://community.cumulocity.com/u/system)\
**Post date:** [April 14, 2024, 8:45am UTC](https://community.cumulocity.com/t/global-variable-shared-between-functions/7458/4 "2024-04-14T08:45:30Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
