Shop OBEX P1 Docs P2 Docs Learn Events
To VAR or to DAT ? — Parallax Forums

To VAR or to DAT ?

Nico SNico S Posts: 9
edited 2009-11-22 21:27 in Propeller 1
Hi all,

I have read the VAR and DAT block designator section in manual ver 1.1.
The VAR block designator gives a clear description of the object and cog scope on declared variables.
Im a bit confused on when to declare/use a variable(s) in the DAT or VAR block.
Are there differences in scope between the VAR and DAT block ?

Sorry if this has been asked before.
TIA,
Nico Schoemaker.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-11-22 19:21
    No, there's no difference in scope.

    The main difference is that each instance of the object declared somewhere else has its own VAR area. The DAT blocks are grouped together and allocated only once no matter how many instances of the object there may be.

    Lets say you have an I/O driver that provides access to PC-compatible files on an SD card. Lets say you want to have several files open. You would put any variables that are common to all files into a DAT block. This might include the volume name and maybe some information about the volume directory. Each file would be handled by a separate instance of this object. The variables that have to do with a specific file would be declared in a VAR block. Each declared instance would have its own copy of the file variables. There would be only one copy of the volume related variables and only one copy of the code itself.
  • Nico SNico S Posts: 9
    edited 2009-11-22 19:39
    Hi Mike,

    Thank you, your anwser was very helpful.

    So VAR declared variables can be·seen as·object/instance variables and DAT declared variables·as class/static variables.
    In other words VAR declared variables are private to an instance of a class and DAT declared variables are shared among every instance of the class.

    Best regards,
    Nico Schoemaker.
  • Mike GreenMike Green Posts: 23,101
    edited 2009-11-22 19:41
    Yes
  • JRetSapDoogJRetSapDoog Posts: 954
    edited 2009-11-22 20:14
    Good to know. Thanks for asking and summarizing, Nico, and for the excellent example, Mike. I often learn best from examples.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-11-22 21:27
    This ought to go in the good threads index. Mike's explanation and Nico's summary are both paradigms of clarity!

    -Phil
  • Really usefull thread. There should be a SPIN FAQ to be pinned in the learn thread.
  • Remember, too, that DAT values can be pre-initialized to any value in the source code. Global vars are always cleared to zero.
Sign In or Register to comment.