simple question on parameter passing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Geoff

    #1

    simple question on parameter passing

    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
    passed is of type string? Why no type mismatch error??



    Many thanks

    Geoff


  • Karl E. Peterson

    #2
    Re: simple question on parameter passing

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


    --


    Comment

    • Steve Gerrard

      #3
      Re: simple question on parameter passing


      "Karl E. Peterson" <karl@mvps.orgw rote in message
      news:edpo6h$tfd $1@emma.aioe.or g...
      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 passed 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

      >
      IN fact, when you pass TxtNum1, you are passing 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 passes the result. Since default properties can also get you
      in trouble, it is usually considered better to use TxtNum1.Text directly:
      If IsNumeric(TxtNu m1.Text) Then
      Call Adder(CInt(TxtN um1.Text))
      End If


      Comment

      Working...