Link to home
Start Free TrialLog in
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

asked on

Does VBA do static or dynamic type checking?

Hi,

I'm trying to figure out if VBA does static or dynamic type checking. I can write the following and I know that it will generate an error at 'compile time':

    Dim x As Integer
    x = "hello."

But if we have the explicit option off, we can also declare variables un-typed on the fly, like:

    x = 55;

Now is that being checked at compile time, or does the VBA interpreter try to figure out that x should be represented by an Integer only during run time?

Thanks
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

From VB3 and the introduction of Variants, type checking has become Run-time.
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

ASKER

Hi,

What about my original example though, isn't this caught at compile-time?:

    Dim x As Integer
    x = "hello."

I'll get a 'compile' error before it lets me run the script. Isn't this static type checking?

Thanks
That's interesting. It doesn't with me.

One reason that it is interesting is that this code works without any error because of implicit type conversion:
Dim x as Integer
x = "333"

But this fails at run time;
Dim x as Integer
x = "33e"
To fail at compile time, the compiler would have to rehearse the type conversion to see if it failed.

and what would happen here:?
Dim x as Integer
Dim y as string

y = GetAString() 'function
x = y '???

       

       
 

Hmm ok yeah those all seem to generate run-time errors.

What about this though (consider 'apple' is a class I made):

    Dim MyValue As String
    MyValue = "30"
    Set MyValue = New apple

This generates a:

    "Compile error: Object expected"

so is that still in a way static type-checking? If VBA was completely run-time checking, shouldn't it have generated the error at run-time?

Thanks
Or another example of static type-checking (I think):

Sub main()
    Dim x As String
    x = "50"

    Dim y As String
    y = "100"

    Call takeStrictParams(x, y)
End Sub


Sub takeStrictParams(x As Integer, y As Integer)
    ......
End Sub


That will generate a compile-time error since the sub is expecting certain types.



I guess what I'm really trying to determine is, does VBA have a mix of static and dynamic type checking? I think it does?

Thanks
It still seems to be for run-time only for Type Mismatch. Interestingly this compiles and runs without any errors.


    Dim MyValue 'As String
    MyValue = "30"
    Set MyValue = New apple


Personally, I like to keep types as tight as possible, and do not use 'As Object' or 'As Variant' if it can be avoided.
Hmm I'm using VBA through microsoft office 2003. Are you using some other VB version (I'm new to VBA etc so I've heard there are quite a few different derivatives).

What about the function call example, does that generate a compile time error for you?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

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