[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

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
0
DJ_AM_Juicebox
Asked:
DJ_AM_Juicebox
  • 5
  • 4
1 Solution
 
GrahamSkanCommented:
From VB3 and the introduction of Variants, type checking has become Run-time.
0
 
DJ_AM_JuiceboxAuthor Commented:
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
0
 
GrahamSkanCommented:
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 '???

       

       
 

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
DJ_AM_JuiceboxAuthor Commented:
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
0
 
DJ_AM_JuiceboxAuthor Commented:
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
0
 
GrahamSkanCommented:
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


0
 
GrahamSkanCommented:
Personally, I like to keep types as tight as possible, and do not use 'As Object' or 'As Variant' if it can be avoided.
0
 
DJ_AM_JuiceboxAuthor Commented:
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
0
 
GrahamSkanCommented:
I use VBA and VB6. They are virtually the same in this respect.

Yes. The compiler is able to compare the two type declarations and show a compile-time error if the passing is ByRef. ByVal mismatches can get past the compiler.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now