Shop OBEX P1 Docs P2 Docs Learn Events
undeclaring variables — Parallax Forums

undeclaring variables

Jon LoldrupJon Loldrup Posts: 14
edited 2007-04-12 07:51 in BASIC Stamp
Is it possible to undeclare variables in a BS2? I can't find anything about it in the reference manual.

/jon

Comments

  • allanlane5allanlane5 Posts: 3,815
    edited 2007-04-11 18:04
    You can declare another variable as an 'alias' to an existing variable. Like this:

    MyTemp VAR BYTE

    MyOtherTemp VAR MyTemp

    Why would you want to "undeclare" a variable? Just don't declare it in the first place. You've only got 26 bytes, and 2000 bytes of program storage. Doing something you intend to undo later -- well, there's not a lot of resources available to devote to that sort of thing.
  • Mike GreenMike Green Posts: 23,101
    edited 2007-04-11 18:07
    In PBasic this is known as sharing or the use of an "alias". You can't "undeclare" them, but you can declare a new name that's identical functionally to the old name. Since there are only 13 words (also usable as 26 bytes, 52 "nibbles" or 208 individual bits), the alias idea works reasonably well. If you want to reuse a word as two bytes, you can use an alias to the predefined names like B5 or B6 as in "newByte var B5".
  • Bill ChennaultBill Chennault Posts: 1,198
    edited 2007-04-11 18:08
    jon--

    Couldn't you just reuse one that you have already declared and no longer need, assuming it was the correct type?

    A good example is the variable you must declare as the loop index variable for a FOR/NEXT loop. When the loop is finished, you can set that variable to zero (or any other value up to the maximum type size) and use it for anything you want.

    --Bill

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    You are what you write.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-04-11 19:07
    Jon,

    I find that when a variable is only to be used temporarily it’s good to give it such a name…temp, work or something that doesn’t lock it to a specific use. I re-use variables all the time and try to give those variables useful but more general names such as ioByte or ioData. As has been said previously you can always re-use the variable space for something else. Just remember that it won’t be un-initialized (0) at that point so remember that before you use it. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • Jon LoldrupJon Loldrup Posts: 14
    edited 2007-04-12 07:51
    I see there are quite some options in the alias function.
    that should do the job

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    /jon
Sign In or Register to comment.