Link to home
Start Free TrialLog in
Avatar of victorlong
victorlong

asked on

about NULL

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?
ASKER CERTIFIED SOLUTION
Avatar of wizard2072098
wizard2072098

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of victorlong
victorlong

ASKER

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

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.

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

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

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