?
Solved

Does VBA do static or dynamic type checking?

Posted on 2008-06-15
9
Medium Priority
?
291 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
Technology Partners: 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!

 

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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

765 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