Solved

no compile error while type mismatch

Posted on 2001-06-18
10
202 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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.
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…
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…

821 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