Wich default value for an object?

amorosik

Member
Local time
Today, 01:15
Joined
Apr 18, 2020
Messages
540
Is there a document that describes the default value assumed by an object, declared but never used yet?
For example if I run Dim Var1 as string, what value does that variable contain?
What if it's a boolean? And an Integer?
 
You must define what Var1 is, for example:
Dim Var1 As String
Var1="This String"
It cannot be boolean or an integer because you defined it as a String
 
Not sure there is a document but easy to Google something like ‘vba data types’

or just declare and debug.print

dim v as string
Dim b as Boolean
Dim n as integer
Debug.print v; isnull(v); len(v)
Etc
 
As I understand it, the default values are:
String: ""
Boolean: False
Integer: 0
Long: 0
Byte: 0
Date: #00:00:00#
Variant: Empty
 
I haven't found such a reference but have learned the following:

String = empty string
Boolean = False
any number type = 0
Variant = empty string
 
You must define what Var1 is, for example:
Dim Var1 As String
Var1="This String"
It cannot be boolean or an integer because you defined it as a String

After DIM VAR1 AS STRING wich value is contained on VAR1?
 
For example if I run Dim Var1 as string, what value does that variable contain?
What's stopping you from trying this out yourself with Debug.Print?
"" (vbNullstring) and NULL would have to be specifically asked for, because otherwise you wouldn't see anything.
 
I don't think that's correct
Good point.
Code:
Sub test()
Dim x As Variant
Debug.Print x = ""
Debug.Print x = vbNullString
Debug.Print x = 0
Debug.Print IsDate(x)
Debug.Print IsNull(x)
End Sub
Output:
True
True
True
False
False
 
Interesting - I think there must be some coercion going on, but the result is the result! (y)

Debug.Print IsEmpty(x) will also be True
 
Absolutely there is coercion because of the presence of an "=" - a relational operator - in the expression. That operator coerces the item on the left to be compatible with the item on the right (since the items on the right are constants and CAN'T be coerced.) However, the last two items have no operator so there is nothing to be coerced (as the argument of a diadic operator) and the monadic arguments being passed do not involve coercion either.
 
An unassigned variant is EMPTY not null.
But if it is empty it is also 0, and ""
It is like Schrodinger's Cat
Depending on how it is used it EMPTY, "", 0

Test to confirm
Code:
Public Sub EmptyVariant()
  Dim x As Variant
  Debug.Print IsEmpty(x)
  Debug.Print x = ""
  Debug.Print x = 0
  Debug.Print IsNull(x)
  x = 7
  x = Empty
  Debug.Print x
 
End Sub
 

Users who are viewing this thread

Back
Top Bottom