Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 113
Default Setting variables to Nothing

As good programming practice, what of the possible variable types should be
set to nothing or null?
Can /should you set integers to nothing for example?
Thanks
Matt


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Setting variables to Nothing

integers can't be set to nothing. They are initialized to a value of zero.

You shouldn't have to worry about any type of local variable because they go
out of scope and are destroyed when the code in which they are defined stops
running.

No variables have a default value of Null, so you shouldn't use that.

Only objects can be set to Nothing

Dynamic arrays can be erased

--
Regards,
Tom Ogilvy


"Matt Jensen" wrote in message
...
As good programming practice, what of the possible variable types should

be
set to nothing or null?
Can /should you set integers to nothing for example?
Thanks
Matt




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 113
Default Setting variables to Nothing

Thanks Tom
Pertaining to that checkbox naming code you helped me with yesterday:

snippet

dim cb as OLEObject
Set cb = ws.OLEObjects.Add(ClassType:="Forms.CheckBox.1", _

I reuse / redfine this cb variable/object several times in one procedure,
should I set it to nothing before each I use it again or just at the end of
the proc?

Thanks
Matt

"Tom Ogilvy" wrote in message
...
integers can't be set to nothing. They are initialized to a value of

zero.

You shouldn't have to worry about any type of local variable because they

go
out of scope and are destroyed when the code in which they are defined

stops
running.

No variables have a default value of Null, so you shouldn't use that.

Only objects can be set to Nothing

Dynamic arrays can be erased

--
Regards,
Tom Ogilvy


"Matt Jensen" wrote in message
...
As good programming practice, what of the possible variable types should

be
set to nothing or null?
Can /should you set integers to nothing for example?
Thanks
Matt






  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Setting variables to Nothing

reassigning it to refer to another object is fine. You don't have to set it
to nothing along the way/in between.

If you want to set it to nothing at the end, you can, but when it hits End
Sub, it will be set to nothing by VBA anyway. (if I recall correctly that it
is local to that procedure).

--
Regards,
Tom Ogilvy

"Matt Jensen" wrote in message
...
Thanks Tom
Pertaining to that checkbox naming code you helped me with yesterday:

snippet

dim cb as OLEObject
Set cb = ws.OLEObjects.Add(ClassType:="Forms.CheckBox.1", _

I reuse / redfine this cb variable/object several times in one procedure,
should I set it to nothing before each I use it again or just at the end

of
the proc?

Thanks
Matt

"Tom Ogilvy" wrote in message
...
integers can't be set to nothing. They are initialized to a value of

zero.

You shouldn't have to worry about any type of local variable because

they
go
out of scope and are destroyed when the code in which they are defined

stops
running.

No variables have a default value of Null, so you shouldn't use that.

Only objects can be set to Nothing

Dynamic arrays can be erased

--
Regards,
Tom Ogilvy


"Matt Jensen" wrote in message
...
As good programming practice, what of the possible variable types

should
be
set to nothing or null?
Can /should you set integers to nothing for example?
Thanks
Matt








  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Setting variables to Nothing

Matt,

No need to keep re-setting to Nothing. It is the same variable, and it can
only have one value at any one time.

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Matt Jensen" wrote in message
...
Thanks Tom
Pertaining to that checkbox naming code you helped me with yesterday:

snippet

dim cb as OLEObject
Set cb = ws.OLEObjects.Add(ClassType:="Forms.CheckBox.1", _

I reuse / redfine this cb variable/object several times in one procedure,
should I set it to nothing before each I use it again or just at the end

of
the proc?

Thanks
Matt

"Tom Ogilvy" wrote in message
...
integers can't be set to nothing. They are initialized to a value of

zero.

You shouldn't have to worry about any type of local variable because

they
go
out of scope and are destroyed when the code in which they are defined

stops
running.

No variables have a default value of Null, so you shouldn't use that.

Only objects can be set to Nothing

Dynamic arrays can be erased

--
Regards,
Tom Ogilvy


"Matt Jensen" wrote in message
...
As good programming practice, what of the possible variable types

should
be
set to nothing or null?
Can /should you set integers to nothing for example?
Thanks
Matt










  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Setting variables to Nothing

If you're re-using that object variable for something that may not exist, it
might be better to set it to nothing to re-initialize it.

For example, I put a single checkbox on the activeworksheet:

Option Explicit

Sub Macro1()
Dim myCBX As MSForms.CheckBox

Set myCBX = ActiveSheet.CheckBox1

Set myCBX = Nothing '<-- try commenting this line
On Error Resume Next
Set myCBX = ActiveSheet.checkbox2
On Error GoTo 0

If myCBX Is Nothing Then
MsgBox "not there"
Else
MsgBox myCBX.Caption
End If

End Sub

Try it once with the line commented and once without the line being commented.

====
If you know the object exists, then you don't need this (as both Tom and Bob
wrote), but ....



Matt Jensen wrote:

Thanks Tom
Pertaining to that checkbox naming code you helped me with yesterday:

snippet

dim cb as OLEObject
Set cb = ws.OLEObjects.Add(ClassType:="Forms.CheckBox.1", _

I reuse / redfine this cb variable/object several times in one procedure,
should I set it to nothing before each I use it again or just at the end of
the proc?

Thanks
Matt

"Tom Ogilvy" wrote in message
...
integers can't be set to nothing. They are initialized to a value of

zero.

You shouldn't have to worry about any type of local variable because they

go
out of scope and are destroyed when the code in which they are defined

stops
running.

No variables have a default value of Null, so you shouldn't use that.

Only objects can be set to Nothing

Dynamic arrays can be erased

--
Regards,
Tom Ogilvy


"Matt Jensen" wrote in message
...
As good programming practice, what of the possible variable types should

be
set to nothing or null?
Can /should you set integers to nothing for example?
Thanks
Matt





--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting Multiple Variables with a loop jlclyde Excel Discussion (Misc queries) 6 November 11th 09 09:40 PM
Not at all clear on use of variables and/or object variables JMay-Rke Excel Discussion (Misc queries) 11 July 4th 08 06:36 PM
Confused about setting up functions based on variables? NotExcelingNow Excel Worksheet Functions 5 January 9th 08 07:31 PM
VBA Setting .Value to a date does not respect local system setting Frank_Hamersley Excel Programming 13 July 18th 04 02:51 PM
Setting a print range using variables Mary Branson Excel Programming 3 February 3rd 04 05:23 PM


All times are GMT +1. The time now is 10:10 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"