Solved

no compile error while type mismatch

Posted on 2001-06-18
10
208 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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 Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses
Course of the Month6 days, 17 hours left to enroll

623 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