?
Solved

about NULL

Posted on 1998-11-30
8
Medium Priority
?
227 Views
Last Modified: 2012-05-04
Can you tell me what is NULL?
Apart from VARIANT, can it be used in other types?
Can you also give me an example showing why we need NULL?
0
Comment
Question by:victorlong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 1

Accepted Solution

by:
wizard2072098 earned 150 total points
ID: 1447520
NULL means "waiting to be initialized", whereas EMPTY or BLANK means "currently has no value".  

Dim v as Variant  --  memory reserved, not initialized.  NULL.

v = ""  -- memory initialized, has no value.  EMPTY/BLANK.

v = vbNull   --- memory released, no value. NULL.

It's the difference between actually holding memory or just having a pointer to a memory area that may or may not be used.

The NULL-able variable is used more often in C++, but is a powerful tool. VB used to reject the use of NULLS because there was no way to assign a null in VB until the advent of the variant. But they still have to give you the "vbNull" type in order to allow you the ability to assign a null.

The variant is the only datatype that can hold a null. Many databases will allow nulls in their records, so the VB variant is a good way to coerce a db null into something else, like an empty string.

Using variants is nice, since they do not actually take up memory until they are initialized ( v = "", or v = 0 ), and they release memory when they are set to null.
0
 

Author Comment

by:victorlong
ID: 1447521
Thank you wizard2.

>Dim v as Variant  --  memory reserved, not initialized.  NULL.

So, "Dim i as Integer" also doesn't need the 2 bytes.

>It's the difference between actually holding memory or just having a pointer to a memory area that may or may not be used.

But the memory area pointed by a varable may not be used by other varables....that equals to holded :-)

> "vbNull" type in order to allow you the ability to assign a

vbNull = Null ?

>The variant is the only datatype that can hold a null.

I see now.

Cheers.

Victor
0
 
LVL 1

Expert Comment

by:wizard2072098
ID: 1447522
"Dim v as Variant" would equate to creating a pointer to an uninitialized memory location in C++. Since the variant can contain any datatype, it is impossible to actually "malloc", or allocate, the amount of memory needed. The variant Dim simply creates a pointer to a memory area that will be allocated and populated later.

Unlike the variant, "Dim i as Integer" would also include the malloc to actually allocate the memory area, since the actual amount of memory needed for the variable is known.

The "vbNull" is the built-in constant that can be used to assign NULL to a variant.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Expert Comment

by:vikiing
ID: 1447523
Just to add two words, not only integer but any numeric and user-defined variables have their lengths defined at compile time by allocating memory for them when executable program is created; hence they are NOT NULLable.

0
 

Author Comment

by:victorlong
ID: 1447524
Hi experts

I have just tested that
   Dim v as Variant
   Print VarType(v)
print out EMPTY, instead of NULL! An EMPTY would be 0 or ""???

Cheers.

Victor
0
 

Author Comment

by:victorlong
ID: 1447525

Dim v as Variant     'v got a small space (as Empty type).
v = "string"         'v got space of 16 bytes (as String type).
v = NULL             'v got no space (as Null type).

Am I write?
0
 
LVL 1

Expert Comment

by:wizard2072098
ID: 1447526
>Dim v as Variant     'v got a small space (as Empty type).
  -- v has no allocated memory here. the small space is for the pointer, not the value associated with the pointer, so you really can't count it.
>v = "string"         'v got space of 16 bytes (as String type).
>v = NULL             'v got no space (as Null type).
 

0
 
LVL 1

Expert Comment

by:wizard2072098
ID: 1447527
>I have just tested that
>   Dim v as Variant
>   Print VarType(v)
>print out EMPTY, instead of NULL! An EMPTY would be 0 or ""???

-- the variable is technically EMPTY, but in reality it is "not initialized". That's somewhat different than "NULL". Null is a VALUE, not a STATE (I may have confused you in an earlier post). However, in VB, assigning NULL to a variant causes it to release the memory it is holding, which effectively changes the state of the variable. In C++, for example, assigning NULL to a variable does not release the memory. It just changes the variable's value. In C++ you have to release memory manually. In VB, they've given you sort of a nice "built in" way of doing it.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month8 days, 21 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question