Solved

no compile error while type mismatch

Posted on 2001-06-18
10
197 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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

705 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now