ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   A question about Nothing (https://www.excelbanter.com/excel-programming/281596-question-about-nothing.html)

Dave F[_4_]

A question about Nothing
 
Hi

I understand what Nothing does:

Set MyObject = Nothing

But I don't understand the reason for it.
Especially at the end of a routine

Doesn't End Sub do the same thing?

Dave F.




Phobos

A question about Nothing
 
In an ideal world, The app should tidy up after itself rendering the:

Set Object = Nothing

as an excersize in futility, however some apps, mentioning no names (Access)
have bugs in them which may be avoided by setting object variables to
nothing at the end of the sub or function.

P


"Dave F" wrote in message
...
Hi

I understand what Nothing does:

Set MyObject = Nothing

But I don't understand the reason for it.
Especially at the end of a routine

Doesn't End Sub do the same thing?

Dave F.






Harald Staff

A question about Nothing
 
Hi Dave

No. "Set" allocates memory for something. Say you have an object X that uses 2Mb ram. Then
in theory it goes like this:

Set A as new X '2 Mb allocated for future use
Set B as new X 'and another 2
'.... do stuff with or without A and B, until
Set A as noting 'release A's 2 Mb for use elsewhere
Set B as nothing 'another 2

"In theory" because it doesn't always release, VBA or whatever program. In those cases you
have a "memory leak" and your Pc starts acting sluggish for no obvious or logical reason.
End Sub will or will not release memory, depending on wether the A variable is local or
not, and also on luck (memory leak II ). Anyway it's a very good coding habit to release
memory manually -we do have style after all ;-)
--
HTH. Best wishes Harald
Followup to newsgroup only please.

"Dave F" wrote in message
...
Hi

I understand what Nothing does:

Set MyObject = Nothing

But I don't understand the reason for it.
Especially at the end of a routine

Doesn't End Sub do the same thing?

Dave F.







All times are GMT +1. The time now is 07:15 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com