Go Back   Forum Care Forums > Development Reference Area > Visual Basic

Reply
 
LinkBack Thread Tools Display Modes
simple question on parameter p***ing
Old
  (#1)
Geoff
Guest
 
Posts: n/a
Default simple question on parameter p***ing - 06-04-2007, 10:30 AM

Consider the procedure



Private Sub Adder(ByVal Num1 As Integer, etc. )



End Sub



which called by Call Adder( TxtNum1,.) where TxtNum1 is a text box.





Why does Num1 accept the value as an integer when the actual parameter being
p***ed is of type string? Why no type mismatch error??



Many thanks

Geoff


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: simple question on parameter p***ing
Old
  (#2)
Karl E. Peterson
Guest
 
Posts: n/a
Default Re: simple question on parameter p***ing - 06-04-2007, 10:30 AM

Geoff wrote:
> Consider the procedure
>
> Private Sub Adder(ByVal Num1 As Integer, etc. )
>
> End Sub
>
> which called by Call Adder( TxtNum1,.) where TxtNum1 is a text
> box.
>
> Why does Num1 accept the value as an integer when the actual
> parameter being p***ed is of type string? Why no type mismatch
> error??


That's one of the "features" introduced at VB4, and commonly referred to as
"Evil Type Coercion". If VB can coerce, it does. Here's an article I wrote
on that, way back when:

Coercion Aversion
http://vb.mvps.org/articles/pt199511.pdf

--


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: simple question on parameter p***ing
Old
  (#3)
Steve Gerrard
Guest
 
Posts: n/a
Default Re: simple question on parameter p***ing - 06-04-2007, 10:31 AM


"Karl E. Peterson" <EMAIL REMOVED> wrote in message
news:edpo6h$tfd$EMAIL REMOVED...
> Geoff wrote:
>> Consider the procedure
>>
>> Private Sub Adder(ByVal Num1 As Integer, etc. )
>>
>> End Sub
>>
>> which called by Call Adder( TxtNum1,.) where TxtNum1 is a text
>> box.
>>
>> Why does Num1 accept the value as an integer when the actual
>> parameter being p***ed is of type string? Why no type mismatch
>> error??

>
> That's one of the "features" introduced at VB4, and commonly referred to as
> "Evil Type Coercion". If VB can coerce, it does. Here's an article I wrote
> on that, way back when:
>
> Coercion Aversion
> http://vb.mvps.org/articles/pt199511.pdf
>


IN fact, when you p*** TxtNum1, you are p***ing a TextBox, not a string. VB also
finds the default property of the TextBox, which is Text, which is a string, and
then does ETC and p***es the result. Since default properties can also get you
in trouble, it is usually considered better to use TxtNum1.Text directly:
If IsNumeric(TxtNum1.Text) Then
Call Adder(CInt(TxtNum1.Text))
End If


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top