Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Nothing, Empty and Null

I'm just trying to make sure my understanding is correct he-
Is Nothing a 'value' an object variable can have. I'm an old 'c' programmer
so my guess is it's like having a null pointer?
My guess is that Empty means that the object exists but none of its elements
has any data. Like having allocated memory to a structure and set a pointer
to it but not set values for any of the elements?
I also guess that Null means an unset data element - so an integer can be
+ve, 0, -ve or NULL?
Sorry to be so old fashioned - but I find it really helps if I understand
what's actually going on. Anyway thanks in anticipation and all that ...
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Nothing, Empty and Null

John,

'Nothing' is more of a condition than a value. Internal to COM, as I
understand it , and I am no expert, 'Nothing' indicates that there are no
references to the object. Its internal reference conter is 0. Only
Object-type variables may be 'Nothing'. You test the Nothing condition with
the Is operator.

Dim Obj As Object ' generic Object. Could also be an explicit
' object type
Set Obj = Range("A1")
Debug.Print "Obj In Nothing: " & CStr(Obj Is Nothing)
Set Obj = Nothing
Debug.Print "Obj In Nothing: " & CStr(Obj Is Nothing)


'Empty' applies only to Variant type variable. A Varaint can contain any
type of value (you're an old C programmer -- a Variant is a 'struct' with a
header followed by a 'union' of all supported data types). 'Empty' indicates
that the Variant has never been assigned a value of any type, or has been
explicit cleared using Empty. You can assign the value Empty to Variant:

Dim V As Variant
V = 123
Debug.Print "V is empty: " & CStr(IsEmpty(V))
V = Empty
Debug.Print "V is empty: " & CStr(IsEmpty(V))


'Null' applies only to Varaint variables, and indicates that the variable
contains no valid data and is not any data type. This differs from the
'Empty' in that if a Variant is Null, it cannot be converted to a default
value of a particular data type, as indicated by the context of the
variable's use. That last sentence is awkward, and better said with an
example:

Dim V As Variant
V = Null
Debug.Print "V is Null: " & CStr(IsNull(V))
Debug.Print "V is Empty: " & CStr(IsEmpty(V))

' now set V to Empty. It is comparable to both 0 and vbNullString
' depending on the context in which it is used (Numeric or String
' comparison).
V = Empty
Debug.Print "V is Empty: Is Comparable To 0: " & CStr(V = 0)
Debug.Print "V is Empty: Is Comparable To vbNullString: " & CStr(V =
vbNullString)

When V is Null (test with the IsNull function), it contains no valid data.
It is not Empty. Then V is given a value of Empty. With this value, the
comparisons (V= 0) and (V = vbNullString) both return True because the
Variant is evaluated to the default value (0 or vbNullString), depending on
the data type to which it is being compared.

so an integer can be
+ve, 0, -ve or NULL?


Nope. Only Varaint can Null. A numeric data type (Integer, Long, Double,
etc) will ALWAYS contain a valid value. When the variable is declared, it
gets a value of 0. (Unlike C, VB automatically initializes all variables to
their default type: 0, vbNullString, Empty, or Nothing). If an error such
as an overflow occurs, the variable retains its previous value. For example,

Dim I As Integer
On Error Resume Next
I = 123
I = 100000000
Debug.Print I ' displays 123

I hope this help clear things up and doesn't confuse you even more.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)

"John Pritchard" wrote in message
...
I'm just trying to make sure my understanding is correct he-
Is Nothing a 'value' an object variable can have. I'm an old 'c'
programmer
so my guess is it's like having a null pointer?
My guess is that Empty means that the object exists but none of its
elements
has any data. Like having allocated memory to a structure and set a
pointer
to it but not set values for any of the elements?
I also guess that Null means an unset data element - so an integer can be
+ve, 0, -ve or NULL?
Sorry to be so old fashioned - but I find it really helps if I understand
what's actually going on. Anyway thanks in anticipation and all that ...



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Nothing, Empty and Null

Nothing is an object variable state, that is, it has been set to a valid
object.

Empty is as you say.

To quote help
The Null keyword is used as a Variant subtype. It indicates that a variable
contains no valid data.

An integer cannot be empty or null, by declaring it, it is immediately
initialised as 0, a value. Try this code

Debug.Print "Typename: " & TypeName(y)
Debug.Print "Null: " & IsNull(y)
Debug.Print "Empty: " & IsEmpty(y)
If TypeName(y) = "String" Then
Debug.Print "New: " & (y = "")
ElseIf TypeName(y) = "Long" Then
Debug.Print "New: " & (y = 0)
ElseIf TypeName(y) = "Empty" Then
Debug.Print "New1: " & (y = "")
Debug.Print "New2: " & (y = 0)
End If

change the type for variable y.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"John Pritchard" wrote in message
...
I'm just trying to make sure my understanding is correct he-
Is Nothing a 'value' an object variable can have. I'm an old 'c'

programmer
so my guess is it's like having a null pointer?
My guess is that Empty means that the object exists but none of its

elements
has any data. Like having allocated memory to a structure and set a

pointer
to it but not set values for any of the elements?
I also guess that Null means an unset data element - so an integer can be
+ve, 0, -ve or NULL?
Sorry to be so old fashioned - but I find it really helps if I understand
what's actually going on. Anyway thanks in anticipation and all that ...



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 a cell to 'empty' or blank or null ? KRK New Users to Excel 7 May 7th 08 12:49 AM
find an empty cell or null value Janis Excel Programming 2 September 11th 06 11:58 PM
How do you stop excel from charting empty cells/null values as zer Abe-air Charts and Charting in Excel 3 March 20th 06 11:57 PM
make cell contents equal to null value - not blank, but empty mpierre Excel Worksheet Functions 1 December 29th 04 06:57 AM
Testing for null or empty gwgeller[_3_] Excel Programming 4 January 29th 04 08:34 PM


All times are GMT +1. The time now is 10:20 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"