Solved

Does VBA do static or dynamic type checking?

Posted on 2008-06-15
9
234 Views
Last Modified: 2008-06-20
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
Comment
Question by:DJ_AM_Juicebox
  • 5
  • 4
9 Comments
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21788838
From VB3 and the introduction of Variants, type checking has become Run-time.
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 21805625
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21805901
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
 

Author Comment

by:DJ_AM_Juicebox
ID: 21823623
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:DJ_AM_Juicebox
ID: 21823693
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21823748
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21823770
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
 

Author Comment

by:DJ_AM_Juicebox
ID: 21823778
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
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 21824555
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now