Variant vs Object variable type

L

Len

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

Allen Browne

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top