Wich default value for an object? (1 Viewer)

amorosik

Member
Local time
Today, 22:26
Joined
Apr 18, 2020
Messages
390
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?
 

LarryE

Active member
Local time
Today, 13:26
Joined
Aug 18, 2021
Messages
592
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
 

CJ_London

Super Moderator
Staff member
Local time
Today, 21:26
Joined
Feb 19, 2013
Messages
16,616
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
 

cheekybuddha

AWF VIP
Local time
Today, 21:26
Joined
Jul 21, 2014
Messages
2,280
As I understand it, the default values are:
String: ""
Boolean: False
Integer: 0
Long: 0
Byte: 0
Date: #00:00:00#
Variant: Empty
 

June7

AWF VIP
Local time
Today, 12:26
Joined
Mar 9, 2014
Messages
5,473
I haven't found such a reference but have learned the following:

String = empty string
Boolean = False
any number type = 0
Variant = empty string
 

amorosik

Member
Local time
Today, 22:26
Joined
Apr 18, 2020
Messages
390
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?
 

ebs17

Well-known member
Local time
Today, 22:26
Joined
Feb 7, 2020
Messages
1,946
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.
 

June7

AWF VIP
Local time
Today, 12:26
Joined
Mar 9, 2014
Messages
5,473
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
 

cheekybuddha

AWF VIP
Local time
Today, 21:26
Joined
Jul 21, 2014
Messages
2,280
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
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 15:26
Joined
Feb 28, 2001
Messages
27,188
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.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 16:26
Joined
May 21, 2018
Messages
8,529
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

Top Bottom