Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

no compile error while type mismatch

Posted on 2001-06-18
10
Medium Priority
?
216 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
[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
  • 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

Independent Software Vendors: 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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

688 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