Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

no compile error while type mismatch

Posted on 2001-06-18
10
Medium Priority
?
224 Views
Last Modified: 2010-05-02
i have a function:

MyFunction (Param1 as MyEnum) as Variant

While i call the function like this:
lsReturn = MyFunction("123")

MyEnum is a long and "123" is a literal string.

How come no compile error is prompt while compile and making a EXE?

0
Comment
Question by:chnelson
  • 4
  • 4
  • 2
10 Comments
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 6201723
It is by design in MS way. Look at this link:

http://msdn.microsoft.com/library/devprods/vs6/vbasic/vbcon98/vbconpassingargumentstoprocedures.htm

There is a similar example, as you your case.

Cheers.
0
 
LVL 15

Expert Comment

by:ameba
ID: 6201742
Note that you are passing parameter "123" ByVal.
You are not passing string variable, but calculated value.

Option Explicit
Private Enum MyEnum
    dsNoRecords = 123
    dsNothingSelected
    dsNormal
    dsModify
    dsAdd
End Enum

Private Sub Form_Click()
    Dim lsReturn As Variant, x As String
    x = "123"
    lsReturn = MyFunction(x)     ' type mismatch error
   
    lsReturn = MyFunction((x))   ' no error, you are passing value of x
   
    lsReturn = MyFunction(x * 1) ' no error, you are passing calculated value
   
End Sub

Private Function MyFunction(Param1 As MyEnum) As Variant
    Beep
End Function
0
 
LVL 15

Expert Comment

by:ameba
ID: 6202582
If you pass parameter by value, VB compiler will insert converson function automatically.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 9

Expert Comment

by:Valliappan AN
ID: 6203081
A nice explanation by ameba. :)
0
 
LVL 1

Author Comment

by:chnelson
ID: 6203158
ameba,

is it some way to disable that kind of auto convertion?

This kind of weak type checking is really dangerous which cause lots of run-time error before delopying the EXE to customers.
0
 
LVL 1

Author Comment

by:chnelson
ID: 6203163
ameba,

is it some way to disable that kind of auto convertion?

This kind of weak type checking is really dangerous which cause lots of run-time error before delopying the EXE to customers.
0
 
LVL 15

Accepted Solution

by:
ameba earned 40 total points
ID: 6203321
>is it some way to disable that kind of auto convertion?

Type checking is present if you pass variable ByRef.

VB will do many things 'behind the scene' to allow easy and fast programming.  If you remove 'Option Explicit' you don't have to declare variables.

Of course this can be dangerous, many VB programmers asked for 'Option Strict' or 'Option Anal Strict' to disable those 'features'...
0
 
LVL 1

Author Comment

by:chnelson
ID: 6206583
oh...
i got it...
asking you for a comment ameba,
to enforce strong type checking,
is it a good way to use ByRef instead of ByVal?
the only side-effect i think is that the parameters (ByRef) may be modified accidentally...
0
 
LVL 15

Expert Comment

by:ameba
ID: 6206660
>to enforce strong type checking,
>is it a good way to use ByRef instead of ByVal?

No, use ByVal if you need to pass parameter one-way [in]
use ByRef if you need to pass parameter two-way [in]+[out]

--
Please, maintain your old questions! You still have many opened questions.
0
 
LVL 1

Author Comment

by:chnelson
ID: 6207087
thanks ameba, very clear explaination
0

Featured Post

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!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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

886 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