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 ...



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Nothing, Empty and Null

John,

Shocked that overflow doesn't give runtime error


Overflow most certainly does cause a run time error. The example code I gave
had an "On Error Resume Next" statement, which causes VB/VBA to ignore the
error and continue execution as if no error had occurred. See
http://www.cpearson.com/excel/ErrorHandling.htm for a discussion of VB/VBA
Error Handling. (On Error Resume Next is dangerous and very often horribly
misused -- it does NOT FIX any errors, it simply ignores them and continues
code execution. I've seen projects in which every procedure has "On Error
Resume Next" at the top of the procedure, and no other error checking. A
debugging nightmare and horrible code.) The code was there to illustrate
that even in an error condition, an Integer (or any numeric data type) will
always contain a valid value, although in an error condition it may not be
the value you expect.

I've not used variants
before - but now I see their use if I need Null


Variant are not restricted to VB/VBA. They are part of COM/OLE/Automation
subsystem and supported by Microsoft Foundation Classes (MFC). You can use
COleVaraints in C++ if you want. See
http://msdn2.microsoft.com/zh-tw/lib...hw(VS.80).aspx



--
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
...
Many Many thanks Chip and sorry if this is a repeat post!
I get the object nothing OK
I've also had a play round with your excellent e.g's. I've not used
variants
before - but now I see their use if I need Null (or empty) to indicate an
uninitialised value. I get the subtle Empty/Null distinction - Empty is
default and can change data type - have to set to null and then can't set
to
another data type. Shocked that overflow doesn't give runtime error -
could
have been massively confusing without info you give. Going to add post on
strings - had to parse a string in VB and really annoyed at using
right/left
to extract individual characters - hope there's another way.
Thanks again for the really useful reply! JP.

"Chip Pearson" wrote:

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
...






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 02:53 PM.

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

About Us

"It's about Microsoft Excel"