Variant vs Object variable type

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am elaborating the ways to replace in my program variant variable
declarations with the object one. For example, if I have

Dim A as Variant
A=5
A="Hello"

I intend to have:
Dim A as Object

and to assign to A a number and a string.

How it can be done?

Thank you.
 
An object is something (like a form or text box) that has properties and
methods.

5 is not an object: it is a number (probably integer.)
"Hello" is not an object: it is a string.

Therefore you could code:
Dim A As Object
Set A = Forms(0)
but you cannot code:
Set A = 5
because 5 is not an object.

In general, you want to declare the most specific data type you can. So if
you will assign a text box to the control, it would be better to use:
Dim A As Control
rather than:
Dim As As Object
Better yes:
Dim A As Textbox

Likewise, it is better to use:
Dim A As Long
if you plan to assign 5 to it.
However, you do need to use Variant where the value may be Null.
 
Back
Top