Solved

Does VBA do static or dynamic type checking?

Posted on 2008-06-15
9
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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
 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

696 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