Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to use a collection as 'global' variable

Subject: How to use a collection as 'global' variable

From: Alon

Date: 11 Sep, 2011 09:15:31

Message: 1 of 2

Hi,

I have a collection which is of class 'containers.Map'. It contains objects of a certain class which I created.
I can set this collection to be 'global' and share it between the main function and all sub functions. But I didn't find a way to do the same with the objects that this collection refers to. So, what ever change I make to one of the objects I have to re-enter it to the collection to keep it shared.

Is there a simple way to make the objects 'global' as well as the collection?

Subject: How to use a collection as 'global' variable

From: Rob Comer

Date: 12 Sep, 2011 14:05:11

Message: 2 of 2

"Alon " <utzly@bgu.ac.il> wrote in message <j4hu7i$3mj$1@newscl01ah.mathworks.com>...
> Hi,
>
> I have a collection which is of class 'containers.Map'. It contains objects of a certain class which I created.
> I can set this collection to be 'global' and share it between the main function and all sub functions. But I didn't find a way to do the same with the objects that this collection refers to. So, what ever change I make to one of the objects I have to re-enter it to the collection to keep it shared.
>
> Is there a simple way to make the objects 'global' as well as the collection?

I don't think it's a question of global vs. local.

It sounds as if you are working with value objects, and that you are assigning an object from the collection to a variable, and then changing one or more property values. This would mean that you have actually changed a copy of the object, so naturally you then have to enter the copy into the collection in place of the original, otherwise you won't be changing the collection itself.

This would not happen with handle objects; instead you'd store a handle in the collection and update the one and only copy of the object via the handle. You could change your class to inherit from handle, but you should do this only if it makes architectural sense for your broader application to deal with handle objects instead of value objects.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us