03-10-2011, 01:44 PM
I don't know whether this is expected behaviour or not because of the way Strings are stored, but my high score routine just presented this issue so I thought I'd check before I changed how it works. :-)
If I declare a string outside of a sub I can use it within any sub. If I change the value within the sub then that's fine, unless I make it equal to another String which was declared within that sub. If I do that, the value of the first string becomes blank as soon as the sub ends.
This is kind of hard to explain, so see this example:
The output from this is:
During the sub the value of testglobal is "local". Once the sub ends, it becomes blank.
I think I would be better off using a function for this kind of thing rather than a sub, but I just thought it was worth checking whether that behaviour was expected or not.
If I declare a string outside of a sub I can use it within any sub. If I change the value within the sub then that's fine, unless I make it equal to another String which was declared within that sub. If I do that, the value of the first string becomes blank as soon as the sub ends.
This is kind of hard to explain, so see this example:
Code:
dim testglobal as string
cls
testglobal = "global"
print testglobal
setlocal()
print testglobal
print "done"
sub setlocal
dim testlocal as string
testlocal = "local"
testglobal = testlocal
print testlocal
print testglobal
end sub
Code:
global
local
local
done
I think I would be better off using a function for this kind of thing rather than a sub, but I just thought it was worth checking whether that behaviour was expected or not.