Local Variables' lifetime

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
myModule.GenerateCodes(Quantity)

// if generated codes contain duplicates, generate new batches
// until no duplicates is found
while myModule.IsDuplicate
  myModule.GenerateCodes(Quantity)
wend

myModule.UpdateText(wMain.charCodeField)
wQuantityChooser.Close

else
tfCodeQuantity.Text = “”
tfCodeQuantity.CueText = “Enter a valid number”
end if[/code]

It is destroyed.

If you declare a variable inside a loop then it is destroyed when the loop ends too.

Just follow it in the debugger to see what happens :wink:

Answered 10 sec after it was posted. New record?

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
  return r
End Function

[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:

[code]
dim v as Integer = MyFunction()

Function MyFunction () As Integer
dim r as integer = 5
return r
End Function
[/code][/quote]
Are you sure? Isn’t just the value returned, and the variable r destroyed? (Not at my computer)

Understood!
Thanks.

I guess what you said is the true one.

That seems inefficient, but I don’t know the implementation details there., so imagine my example function is returning a Dictionary or a String instead.

He isn’t returning Quantity, and even if he was Quantity (like r in your example) is an integer, which is a Scalar and not an object.

But you know that much better than I do, so I presume you haven’t had your morning coffee yet :wink:

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.

Ok, home again. “Typing” in the bus is awkward.

If he is returning an object then the object is stored in a variable in the calling method. The local variable would still be destroyed.

Well, you gave me an idea for the next Tips&Tricks column :wink:

Actually, strings are scalars too.

Which begs the question: is the new text type an object?

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 :slight_smile: ) 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.

I highly doubt they are. That would be very inefficient.