Variable scoping

Altair Forum User
Altair Forum User
Altair Employee
edited October 2020 in Community Q&A

 

Submitted by Joe on Wed, 04/20/2011 - 21:23 


When I copy a compound block with variables, the new instances have renamed variables. Is there a way to prevent that?

Tagged:

Answers

  • Altair Forum User
    Altair Forum User
    Altair Employee
    edited June 2016

    Submitted by Anders89 on Wed, 04/20/2011 - 21:44.

    Yes. The renaming only happens to global variables that are defined within a compound block. Because a variable can only have one defining instance, the paste of a 2nd instance causes the rename to happen. The solution is to use local or definition scoped variables that can not be 'seen' outside of the compound. Then there is no renaming that takes place on the copy/paste. The following is taken from the VisSim help file on variable name scoping:

    You can define which portions of the diagram can reference a variable by designating its scope. There are three types of scope: level scope, definition scope, and diagram (or global) scope. To limit the scope of a variable, simply prefix the name with one or two colons (:).

    Level Scope

    A variable with level scope cannot be referenced outside of its current compound block level. Variables with hierarchical level scope are referred to as local variables. Although level scoping is the most restrictive type of scope, it has several advantages. By limiting the region over which variables can be referenced, you can construct sections of a diagram without worrying about whether your variable names conflict with other names used in other parts of the diagram. In addition, users reading your diagram will know immediately that the variables’ use is limited to a small region.

    Variables with level scoping are prefixed with the colon (:) character. I.e. ':foo'

    Definition Scope

    Giving a variable definition scope allows the variable to be referenced not only in the compound block level of the diagram at which it is defined, but also at all levels beneath it. If compound block A contains a definition scope variable, all the sublevels below the compound block are able to reference and use the variable.

    By using definition-scoped variables, you can copy or add subsystems to an existing diagram without breaking or misdirecting references.

    To give a variable definition scope, prefix the name with two colon (::) characters. I.e. '::foobar'