PDA

View Full Version : undeclaring variables



Jon Loldrup
04-12-2007, 12:57 AM
Is it possible to undeclare variables in a BS2? I can't find anything about it in the reference manual.

/jon

allanlane5
04-12-2007, 01:04 AM
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 Green
04-12-2007, 01:07 AM
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 Chennault
04-12-2007, 01:08 AM
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 Savage
04-12-2007, 02:07 AM
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 Loldrup
04-12-2007, 02:51 PM
I see there are quite some options in the alias function.
that should do the job

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
/jon