I have a button, which its code for the ACTION event is posted below.
I declared Quantity just for clarity of reading the code later.
My question is, when the action is done, is Quantity destroyed? or does it still occupy memory?
[code] // checks to see if the entered number is a valid integer, not alphabetical or decimal
// alerts user if wrong input is entered
if IsNumeric(tfCodeQuantity.Text) and InStr(tfCodeQuantity.Text, “.”) = 0 _
and InStr(tfCodeQuantity.Text, “,”) = 0 then
[b]dim Quantity as integer = CDbl(tfCodeQuantity.Text)[/b]
// generage codes
// if generated codes contain duplicates, generate new batches
// until no duplicates is found
tfCodeQuantity.Text = “”
tfCodeQuantity.CueText = “Enter a valid number”
[quote=164557:@Kem Tekinay]Only locally created variables go out of scope (that is, goes away) as soon as a method ends. The exception is if you pass that variable back in the Return statement of a function. For example:
dim v as Integer = MyFunction()
Function MyFunction () As Integer
dim r as integer = 5
Are you sure? Isn’t just the value returned, and the variable r destroyed? (Not at my computer)
Right. All this ties back to reference-counting. When there are zero references to some data, it goes away.
As I said, String or Dictionary would have been a better illustration, both of which are passed by reference. And it was just meant as some additional information for the Radium to help him understand the issue.
Strings are scalars, but are passed as references, which makes me think that all scalars are passed that way. But that’s an implementation detail to which we are not privy.
Yes, you’re right. I am thinking about the data that the variable points to, but if a variable goes out of scope, like when a method ends, that variable is destroyed.
The exception to that (here I go again ) is if you dim the variable as a static. That variable, and it’s value, will survive repeated calls to the same method and will be treated as if you are accessing a module property.